一、通过方法判断字符串的某些特征
isEmpty //判断字符串是否为空
equals //判断两个字符串是否相等
startsWith //判断字符串是否以目标串开头
endsWith //判断字符串是否以目标串结尾
contains //判断字符串是否包含目标串
例如:
String hello = "Hello World. ";
// isEmpty方法判断该字符串是否为空串
boolean isEmpty = hello.isEmpty();
System.out.println("isEmpty = "+isEmpty);
// contains方法判断该字符串是否包含了目标串
boolean contains = hello.contains("or");
System.out.println("contains = "+contains);
此类判断最终返回true或false,【注】使用此类方法的变量类型为布尔
二、字符串长度和位置(字符)的判断
length //判断字符串的长度
charAt //判断字符串指定位置的字符
indexOf //返回目标串(字符)在该字符串首次找到的位置
lastIndexOf //返回目标串(字符)在该字符串最后找到的位置
例如:
String hello = "Hello World. ";
// length方法返回该字符串的长度
int length = hello.length();
System.out.println("length = "+length);
// indexOf方法返回目标串(字符)在该字符串中第一次找到的位置
int index = hello.indexOf("l");
System.out.println("index = "+index);
三、修改字符串内容的方法
toLowerCase \\转化为小写
toUpperCase \\转化为大写
trim \\去掉首尾空格
concat \\末尾添加目标串
substring \\通过位置截取子串
replace \\把原串替换成目标串