常用方法
1. length() 字符个数
2. equals equalsIgnoreCase方法
3. trim方法
4. substring方法
5. concat()方法用于将指定的字符串参数连接到字符串上。
6. contains() 判断是否包括某字符串
7. indexOf()返回子串的位置索引,没有返回-1
8. lastIndexOf() 返回子串的位置索引,没有返回-1
9. replace()替换返回
10. split()拆分为String[]
11. toLowerCase() toUpperCase()转换小写大写
接下来我们看实践
public static void main(String[] args) {
String zz = "hellow";
String zx = "hellow";
String zc = " HEL LOW ";
String zd = " HEL LOW word are you sure";
System.out.println(zx.equals(zz));//equals方法判断两个字符串
// 里的呢内容是否相等。
System.out.println(zx.equalsIgnoreCase(zc));//equalsIgnore
// Case方法判断两个字符串里的呢内容是否相等,且忽略大小写。
System.out.println(zc.trim());//trim方法删除头尾的空白符即“ ”,
// 是无参方法,没有返回值
System.out.println("返回值:" + zc.substring(4));//substring()
//方法返回自定义下标之后的子字符串。
System.out.println("返回值:" + zc.substring(4, 7));//substring()
//方法返回自定义下标范围之间的子字符串。
System.out.println(zz.concat(" word"));//concat()作用与"+"运算
// 符作用相同,用于连接字符串
System.out.println(zz.contains("llow"));//contains()方法用于查找
//字符串是否包含所搜索的字符串,并返回一个boolen值
System.out.println(zc.indexOf("L"));//indexOf()方法会所要查找数
// 据第一次出现的起始下标位置,如果找不到就返回 -1
System.out.println(zd.indexOf("you", 6));//从指定
// 的下标值处开始,返回第一次出现的指定子字符串在此字符串中的索引(即下标值)。
System.out.println(zx.lastIndexOf("ll"));// lastIndexOf()方法
// 返回指定字符在此字符串中最后一次出现处的索引,即与indexof()索引顺序相反,从
// 末尾开始索引查找。
System.out.println("-".repeat(35));
String aa = "wWw.an_tc.1_6.Com";
System.out.println("分隔符设置后的返回值");
for (String s : aa.split("\\.", 3)) {
System.out.println(s);
}//split方法设置(. , $ | *)等转义字符为分割符,必须加\\。
System.out.println("分隔符设置后的返回值");
for (String s : aa.split("_", 3)) {
System.out.println(s);
}//split方法设置( _ - = +)等字符为分割符,时只需""包起来。
System.out.println("分隔符设置后的返回值");
for (String s : aa.split("an|tc", 3)) {
System.out.println(s);
}///split方法还可以设置(an,tc)等自定义字符字符为分割符。
System.out.println(aa.toLowerCase(Locale.ROOT));//toLowerCase()
// 方法将所作用的字符串全部转化为小写
System.out.println(aa.toUpperCase(Locale.ROOT));//toLowerCase()
// 方法将所作用的字符串全部转化为大写
}
接下来看实践结果:
true
false
HEL LOW
返回值:L LOW
返回值:L L
hellow word
true
4
20
2
-----------------------------------
分隔符设置后的返回值
wWw
an_tc
1_6.Com
分隔符设置后的返回值
wWw.an
tc.1
6.Com
分隔符设置后的返回值
wWw.
_
.1_6.Com
www.an_tc.1_6.com
WWW.AN_TC.1_6.COM