String类常用方法总结(二)

2014-11-24 02:29:15 · 作者: · 浏览: 1

运行结果:
97 98 99 100
15. indexOf(int ch):返回指定字符在此字符串中第一次出现处的索引
int t = str1.indexOf(96);
System.out.println(t);
int i = str1.indexOf(97);
System.out.println(i);
int k = str1.indexOf(99);
System.out.println(k);
int f = str1.indexOf(101);
System.out.println(f);
运行结果:
-1
0
2
-1
16. indexOf(int ch, int fromIndex):返回在此字符串中第一次出现指定字符处的索引,从指定的索引开始搜索
int i = str2.indexOf(97, 3);
System.out.println(i);
int t = str2.indexOf(99, 1);
System.out.println(t);
int k = str2.indexOf(97,5);
System.out.println(k);
运行结果:
4
2
-1
17. indexOf(String str):返回指定子字符串在此字符串中第一次出现处的索引
int t = str2.indexOf("a");
System.out.println(t);
int i = str2.indexOf("c");
System.out.println(i);
int k = str2.indexOf("d");
System.out.println(k);
int f = str2.indexOf("e");
System.out.println(f);
运行结果:
2
3
-1

18. indexOf(String str, int fromIndex):返回指定子字符串在此字符串中第一次出现处的索引,从指定的索引开始
int i = str2.indexOf("a", 3);
System.out.println(i);
int t = str2.indexOf("c", 1);
System.out.println(t);
int k = str2.indexOf("a",5);
System.out.println(k);
运行结果:
2
-1

19. isEmpty():当且仅当 length() 为 0 时返回 true
boolean isempty1 = str1.isEmpty();
System.out.println(isempty1);
boolean isempty2 = "".isEmpty();
System.out.println(isempty2);
运行结果:
false
true

下面几个方法与上面indexOf()是相对应的,只不过是返回最后一次出现的索引
20. lastIndexOf(int ch)
int i = str2.lastIndexOf(97);
System.out.println(i);
int k = str2.lastIndexOf(101);
System.out.println(k);
运行结果:
4
-1
lastIndexOf(int ch, int fromIndex)
lastIndexOf(String str)
lastIndexOf(String str, int fromIndex)

21. substring(int beginIndex):返回一个新的字符串,它是此字符串的一个子字符串。该子字符串从指定索引处的字符开始,直到此字符串末尾
String s1 = str2.substring(4);
System.out.println(s1);
String s2 = str2.substring(2);
System.out.println(s2);
运行结果:
abcd
cdabcd
22. substring(int beginIndex, int endIndex):返回一个新字符串,它是此字符串的一个子字符串。该子字符串从指定的 beginIndex 处开始,直到索引 endIndex - 1 处的字符
String s1 = str2.substring(4,7);
System.out.println(s1);
String s2 = str2.substring(2,7);
System.out.println(s2);
运行结果:
abc
cdabc
23. toCharArray():将此字符串转换为一个新的字符数组
char[] c = str1.toCharArray();
for (int i=0;i System.out.print(c[i]+"\t");
}
运行结果:
a b c d
24. toLowerCase():将此 String 中的所有字符都转换为小写
String str3 = "ABCDABCD";
String str4 = "AcbdABcD";
String s1 = str3.toLowerCase();
System.out.println(s1);
String s2 = str4.toLowerCase();
System.out.println(s2);
运行结果:
abcdabcd
acbdabcd

下面是main函数:

public static void main(String[] args) {
String str1 = "abcd";
String str2 = "abcdabcd";
int len = str1.length();
System.out.println("字符串的长度:"+len);
// for (int i=0;i // char c =str.charAt(i);
// System.out.println("下标"+i+"的值:"+c);
// }


// for (int i=0;i // int k = str.codePointAt(i);
// System.out.println("下标"+i+"的值:"+k);
// }


// int k = str.codePointBefore(len);
// System.out.println("下标"+len+"之前的值:"+k);

// int k = str.codePointCount(0, len);
// System.out.println("0到"+len+"之前的值:"+k);
// int k = str.codePointCount(0, len-2);
// System.out.println("0到"+(len-2)+"之前的值:"+k);

// int i = str.compareTo("abd");
// System.out.println(i);
// int k = str.compareTo("abf");
// System.out.println(k);
// int t = "abd".compareTo(str);
// System.out.println(t);
// int j = "abf".compareTo(str);
// System.out.println(j);
// int g = str.compareTo(str);
// System.out.println(g);
// int f = str.compareTo("abcdefg");
// System.out.println(f);
// int r = "abcdefg".compareTo(str);
// System.out.println(r);

// int i = str.com