String类的本质
String类的本质是字符数组
String的API应用
1. 获取字符串长度
字符串变量.length()
String s = "hello wjh";
s.length(); //获取字符串的长度 结果为 9
2.去除字符串的空格
字符串变量.trim();
public class StringLength {
public static void main(String[] args) {
String s = " hello wjh ";
System.out.println("'" + s + "'");
s = s.trim(); //去除字符串前后的空格
System.out.println("'" + s + "'");
}
3.字符串分割
String[] split(String regex);
String[] split(String regex, int limit);
regex: 指定的分割符
limit: 指分割后生成的字符串的个数,即生成的数组的长度。若不指定,表示不限制分割后的字符串个数,直到将整个字符串分割完。
String[]: 方法返回值是字符串数组
public class StringSplit {
public static void main(String[] args) {
String s = "hi,hello,nihao,aligaduo,kongbangwa";
String str = ",";
String[] ss1 = s.split(str);
System.out.println("ss1字符串数组的长度为:"+ss1.length);
for(int i=0; i<ss1.length; i++) {
System.out.print(ss1[i] + " ");
}
System.out.println();
String[] ss2 = s.split(str, 4);
System.out.println("ss2字符串数组的长度为:"+ss2.length);
for(int i=0; i<ss2.length; i++) {
System.out.print(ss2[i] + " ");
}
}
}
4. 转化大小写
String toLowerCase();
String toUpperCase();
String: 返回值是字符串类型
5. 字符串截取
String substring(int beginIndex);
String substring(int beginIndex, int endIndex);
String: 返回一个新的字符串,它是被截取字符串的一个子字符串
beginIndex:截取字符串的开始位置,包含它
endIndex:截取字符串的结束位置,不包含它
endIndex-beginIndex:子字符串的长度
6. 字符串连接
- 使用 “+”连接字符串
- 使用concat() 方法
String concat(String str);
str : 要连接的字符串
String : 返回一个新的字符串
7. 字符串比较
-
"==" equals() 和 equalsIgnoreCase()
(1) “==” :比较内容和地址, 内容和地址相同,返回true,反之,为false
(2)equals() : 只比较内容,与地址无关。内容相同返回true,反之,为false
(3) equalsIgnoreCase() : 将一个String 与 另一个String作比较,不考虑大小写。如果两个字符串的内容相同(忽略大小写),返回true,反之,为falsepublic boolean equals(Object anObject) public boolean equalsIgnoreCase(String anotherString)
boolean : 返回值是布尔类型
anObject : 指被比较的对象
anotherString : 指被比较的字符串 -
compareTo() 和 compareToIgnoreCase()
compareTo()方法按字典序比较两个字符串。compareToIgnoreCase() 方法按字典序比较两个字符串,不考虑大小写。public int compareTo(String str) public int compareToIgnoreCase(String str)
返回值:如果参数字符串等于此字符串,则返回值0;如果此字符串按字典顺序小于字符串参数,则返回一个小于0的值;如果str1 > str2 ,则返回一个大于0的值。
-
startsWith() 和 endsWith()
startsWith()方法是测试字符串是否以指定的前缀开始。
endsWith() 方法是测试字符串是否以指定的后缀结束。public boolean startsWith(String prefix) public boolean endsWith(String suffix)
boolean : 返回指类型
prefix : 指定的前缀字符串
suffix : 指定的后缀字符串
8. 字符串查找
- charAt() 方法
str.charAt(5); //相当与c++中的 str[5], 但是java中不能这样用
- indexOf()方法
str.indexOf("wjh"); //返回字符串"wjh"第一次出现的位置,没有则返回 -1 str.indexOf('w', 5); //从第五个字符开始找(包括第五个字符),返回'w'第一次出现的位置,没有则返回 -1
- lastIndexOf() 方法
str.lastIndexOf("wjh"); //返回字符串"wjh"最后一次出现的位置,没有则返回 -1 str.lastIndexOf('w', 5); //从第五个字符向前开始找(包括第五个字符),返回'w'最后一次出现的位置,没有则返回 -1
9. 字符串替换
-
replace()
str = str.replace('w', 'W'); //str中的w 被替换为 W, 参数类型为 char
-
replaceFirst()
str = str.replaceFirst("wjh", "WJH"); //替换字符串中匹配的第一个"wjh"为"WJH"
-
replaceAll()
str = str.replaceAll("wjh","WJH"); //将自符串中所有的 "wjh" 替换为"WJH"
字符串解析
String类中提供了 matches() 方法,用于检查字符串是否匹配给定的正则表达式。
Scanner scan = new Scanner(System.in);
String read = scan.nextLine(); //读取输入的数据
String regex = "^[a-zA-Z_]{1,}[0-9]{0,}@(([a-zA-Z0-9]-*){1,}\\.){1,3}[a-zA-Z\\-]{1,}$";
boolean b = read.matches(regex);
字符串的类型转换
1. 字符串转换为数组
- toCharArray()
String str = "hello wjh"; char[] c = str.toCharArray(); //将字符串转化为字符数组
2. 基本数据类型转换为字符串
- valueOf() (此为静态方法)
boolean b = true; Stirng s = String.valueOf(b); //boolean类型转换为字符串类型
3. 格式化字符串
- format()
String.format("32的八进制: %o", 32); String.format("%1$d, %2$s, %3$f", 125, "ddd", 0.25); //生成新的字符串。百分号后的1指对第几个参数格式化,$后的d指定转换符类型