/**
* String 常用方法
*/
@Test
void StringMethod() {
String str = "Hello, String 8899 ";
// length() 获取字符长度
System.out.println("length(): " + str.length());
// str.trim() 去除字符中最后的空字符
System.out.println("trim(): " + str.trim());
// equals() 判断两个字符是否相同(区分大小写)
System.out.println("equals(): " + "Hello string ".equals("Hello String "));
// 判断两个字符是否相同(不区分大小写)
System.out.println("equalsIgnoreCase(): " + "Hello string ".equalsIgnoreCase("Hello String "));
// isEmpty() 判断字符是否为空,
System.out.println("isEmpty(): " + str.isEmpty());
// substring() 截取字符串
// 从第二位截取到第七位
System.out.println("substring(): " + str.substring(1, 6));
// 从第七位开始截取到最后
System.out.println("substring(): " + str.substring(6));
// replaceAll() 替换字符
System.out.println("replaceAll():" + str.replaceAll(" ", "_"));
// replace()
System.out.println("replace(): " + str.replace('8', '0'));
// startsWith() 判断字符是否为前缀
System.out.println("startsWith(): " + str.startsWith("Hello"));
// toLowerCase() 将字符转为小写
System.out.println("toLowerCase(): " + str.toLowerCase());
// toUpperCase() 将字符转为大写
System.out.println("toUpperCase(): " + str.toUpperCase());
// concat() 拼接字符
System.out.println("concat(): " + str.concat("+1"));
// contains 判断字符串中是否包含某个字符
System.out.println("contains(): " + str.contains("99"));
// split() 切割字符串
System.out.println("split(): " + str.split(","));
System.out.println("split: " + Arrays.asList(str.split(",")));
}
运行结果