文章目录
- 1.String 类的定义
- 2.String 类的常用方法
- 1. char charAt(int index)返回字符串中第 index 个字符
- 2. boolean equalsIgnoreCase(String other) 如果字符串与other相等(忽略大小写),则返回 true
- 3. int indexOf(String str) lastIndexOf() 查找指定字符串出现的位置
- 4. int length()返回字符串的长度。
- 5.String replace(char oldChar,char newChar)返回一个新串,它 是 通过 用 newChar 替 换 此字 符 串中 出 现的 所 有oldChar 而生成的
- 6.boolean startsWith(String prefix)如果字符串以 prefix 开始,则返回 true
- 7.boolean endsWith(String prefix) 如果字符串以 prefix 结尾,则返回 true
- 8.String substring(int beginIndex),String substring(int beginIndex,int endIndex)返回一个新字符串,该串包含从原始字符串 beginIndex 到串尾或 endIndex-1的所有字符
- 9.String toLowerCase()返回一个新字符串,该串将原始字符串中的所有大写字母改成小写字母
- 10.String toUpperCase()返回一个新字符串,该串将原始字符串中的所有小写字母改成大写字母
- 11.String trim() 返回一个新字符串,该串删除了原始字符串头部和尾部的空格
1.String 类的定义
String 是不可变字符序列
String类相当于一个char类型数组,数组长度一旦创建不可更改,而且vlaue数组还使用了final修饰
public final class String implements java.io.Serializable, Comparable<String>, CharSequence {
@Stable
private final byte[] value;
}
2.String 类的常用方法
1. char charAt(int index)返回字符串中第 index 个字符
public class Test {//使用方法
public static void main(String[] args) {
String name="abcdef";
System.out.println(name.charAt(1));
}
}
效果图:
2. boolean equalsIgnoreCase(String other) 如果字符串与other相等(忽略大小写),则返回 true
public class Test {//使用方法
public static void main(String[] args) {
String name="abcdef";
System.out.println(name.equalsIgnoreCase("ABCDEF"));
}
}
3. int indexOf(String str) lastIndexOf() 查找指定字符串出现的位置
public class Test {//使用方法
public static void main(String[] args) {
String name="abcdefc";
System.out.println(name.indexOf("c"));//找到返回索引
System.out.println(name.indexOf("I"));//找不到返回-1
System.out.println(name.lastIndexOf("c"));//从后开始往前找
System.out.println(name.indexOf("c",4));//从formIndex开始往后查
System.out.println(name.lastIndexOf("c",4));//从formIndex开始往前查
}
}
效果图:
4. int length()返回字符串的长度。
public class Test {//使用方法
public static void main(String[] args) {
String name = "abcdefc";
System.out.println(name.length());
}
}
效果图:
5.String replace(char oldChar,char newChar)返回一个新串,它 是 通过 用 newChar 替 换 此字 符 串中 出 现的 所 有oldChar 而生成的
public class Test {//使用方法
public static void main(String[] args) {
String name = "abcdefc";
System.out.println(name.replace('c','l'));
}
}
效果图:
6.boolean startsWith(String prefix)如果字符串以 prefix 开始,则返回 true
public class Test {//使用方法
public static void main(String[] args) {
String name = "abcdefc";
System.out.println(name.startsWith("abc"));
}
}
效果图:
7.boolean endsWith(String prefix) 如果字符串以 prefix 结尾,则返回 true
public class Test {//使用方法
public static void main(String[] args) {
String name = "abcdefc";
System.out.println(name.endsWith("efc"));
}
}
效果图:
8.String substring(int beginIndex),String substring(int beginIndex,int endIndex)返回一个新字符串,该串包含从原始字符串 beginIndex 到串尾或 endIndex-1的所有字符
public class Test {//使用方法
public static void main(String[] args) {
String name = "abcdefc";
System.out.println(name.substring(2));
System.out.println(name.substring(2,5));//包含第一个数字所在字符,不包含最后一个数字表示的那个字符
}
}
效果图:
9.String toLowerCase()返回一个新字符串,该串将原始字符串中的所有大写字母改成小写字母
public class Test {//使用方法
public static void main(String[] args) {
String name = "abCDefc";
System.out.println(name.toLowerCase());
}
}
效果图:
10.String toUpperCase()返回一个新字符串,该串将原始字符串中的所有小写字母改成大写字母
public class Test {//使用方法
public static void main(String[] args) {
String name = "abCDefc";
System.out.println(name.toUpperCase());
}
}
效果图:
11.String trim() 返回一个新字符串,该串删除了原始字符串头部和尾部的空格
public class Test {//使用方法
public static void main(String[] args) {
String name = " abCDefc ";
System.out.println(name.trim());
}
}
效果图: