String字符串的17+种方法(trim()、charAt()、concat ()、replace、split()、isEmpty()等)
- String (byte[])
- isEmpty()
- charAt()
- compareTo
- compareToIgnoreCase
- 字符串拼接“+” 与concat 的使用
- startsWith与endsWith的使用
- toUpperCase 与 toLowerCase 的使用
- trim()
- 比较字符串,区分大小写equals 与不区分大小写 equalsIgnoreCase
- 替换replace
- 分割split
- 查找字符串indexOf (从左往右)lastIndexOf(从右往左)
1. String (byte[])
byte [] bs =new byte[]{97,98,99,100};
String s1= new String(bs);
byte [] bs =new byte[]{97,98,99,100};
String s1= new String(bs,0,2);
char [] cs = new char[]{'a','b','c','d'};
String s1 =new String (cs,0,2);
System.out.println( s1);
2. isEmpty()
String s = "";
System.out.println(s.isEmpty());
3 charAt(int n )
String s = "1aiudhgfi";
System.out.println(s.charAt(5));
4. compareTo()
compareToIgnoreCase
String s1 = "1aiudhgfi";
String s2 = "hajiod"
System.out.println(s1.compareTo(s2);
1.第一种
String s1 = "a";
String s2 = "b"
2.第二种
String s1 = "asawgf";
String s2 = "as"
3.第三种
String s1 = "aeawgf";
String s2 = "as"
5. compareToIgnoreCase()
String s1 = "E";
String s2 = "a";
System.out.println(s2.compareToIgnoreCase(s1));
6. “+” 与concat 的使用
String s1 = "E";
String s2 = "a";
System.out.println(s2.compareToIgnoreCase(s1));
7. endsWith与startsWith
String s2= "www.baidu.com";
System.out.println(s2.endsWith("m"));
System.out.println(s2.startsWith(".baidu"));
8. toUpperCase,toLowerCase
String s1 ="asfoijHOAUJF";
System.out.println(s1.toUpperCase());
System.out.println(s1.toLowerCase());
9. trim()
String s1 = " dasf fasdf ";
System.out.println(s1.trim());
10. equals 与 equalsIgnoreCase
String s1= "abc";
System.out.println(s1.equals("ABC"));
System.out.println(s1.equalsIgnoreCase("ABC"));
11. replace
s1= "猴子龙龙龙龙龙";
System.out.println(s1);
System.out.println(s1.replace("龙","🐖"));
12. indexOf 与 lastIndexOf
String s1= "o*u*asfjkoip*asdfo*samf";
int index = s1.indexOf("*");
int index1 = s1.lastIndexOf("*");
System.out.println(index);
System.out.println(index1);
int index2 = s1.indexOf("as",5);
13. indexOf 与 lastIndexOf
String s1= "o*u*asfjkoip*asdfo*samf";
int index = s1.indexOf("*");
int index1 = s1.lastIndexOf("*");
System.out.println(index);
System.out.println(index1);
int index2 = s1.indexOf("as",5);
14. getByte()
String s1 ="gfas";
byte [] bs = s1.getBytes();
for(int i =0;i<s1.length();i++) {
System.out.println(bs[i]);
}
15. toCharArray()
String s1= "goaj";
char [] cs = s1.toCharArray();
for(int i=0;i<s1.length();i++) {
System.out.println(cs[i]);
}
g
o
a
j
16. valueOf ()
17. subString()
String s1= "foriasjavaioasfdj";
s1 = s1.substring(3);
System.out.println(s1);
18 综合应用,见下章,String字符串方法实战训练