自己对字符串的各种方法不太熟悉,今天把主要的方法都试了一遍,简单的总结如下(如有不正确的地方欢迎指正):
String str1 = "abcd";
String str2 = "abcdabcd";
1.length():求字符串的长度
int len = str1.length();
System.out.println("字符串的长度:"+len);
运行结果:
字符串的长度:4
2.charAt(int index):取字符串中指定下标的一个元素
for (int i=0;i
System.out.println("下标"+i+"的值:"+c);
}
运行结果:
下标0的值:a
下标1的值:b
下标2的值:c
下标3的值:d
3. codePointAt(int index):index 处字符的代码点值
for (int i=0;i
System.out.println("下标"+i+"的值:"+k);
}
运行结果:
下标0的值:97
下标1的值:98
下标2的值:99
下标3的值:100
4. codePointBefore(int index):给定索引前面的 Unicode 代码点
int k = str1.codePointBefore(0);
System.out.println("下标"+0+"之前的值:"+k);
java.lang.StringIndexOutOfBoundsException
int k = str1.codePointBefore(1);
System.out.println("下标"+1+"之前的值:"+k);
运行结果:
下标1之前的值:97
int k = str1.codePointBefore(2);
System.out.println("下标"+2+"之前的值:"+k);
运行结果:
下标2之前的值:98
int k = str1.codePointBefore(3);
System.out.println("下标"+3+"之前的值:"+k);
运行结果:
下标3之前的值:99
int k = str1.codePointBefore(len);
System.out.println("下标"+len+"之前的值:"+k);
运行结果:
下标4之前的值:100
5. codePointCount(int beginIndex, int endIndex): 返回此 String 的指定文本范围中的 Unicode 代码点数
int k = str1.codePointCount(0, len);
System.out.println("0到"+len+"之前的值:"+k);
运行结果:
0到4之前的值:4
int k = str1.codePointCount(0, len-2);
System.out.println("0到"+(len-2)+"之前的值:"+k);
运行结果:
0到2之前的值:2
6. compareTo(String anotherString)
先看下面API文档的介绍:
int i = str1.compareTo("abd");
System.out.println(i);
int k = str1.compareTo("abf");
System.out.println(k);
int t = "abd".compareTo(str1);
System.out.println(t);
int j = "abf".compareTo(str1);
System.out.println(j);
int g = str1.compareTo(str1);
System.out.println(g);
int f = str1.compareTo("abcdefg");
System.out.println(f);
int r = "abcdefg".compareTo(str1);
System.out.println(r);
运行结果:
-1
-3
1
3
0
-3
3
7. compareToIgnoreCase(String str)
先看下面API文档的介绍:
int i = str1.compareToIgnoreCase("ABCD");
System.out.println(i);
int f = str1.compareTo("ABCD");
System.out.println(f);
运行结果:
0
32
8. concat(String str):将指定字符串连接到此字符串的结尾
String s1 = str1.concat("ABCD");
System.out.println(s1);
String s2 = str.concat("");
System.out.println(s2);
运行结果:
abcdABCD
abcd
9. copyValueOf(char[] data):返回指定数组中表示该字符序列的 String
静态方法
char[] data = {'a','b','c','d','e','f'};
String s1 = String.copyValueOf(data);
System.out.println(s1);
运行结果:
abcdef
10. copyValueOf(char[] data, int offset, int count):返回指定数组中表示该字符序列的 String
char[] data = {'a','b','c','d','e','f'};
String s1 = String.copyValueOf(data,3,3);
System.out.println(s1);
运行结果:
def
11. endsWith(String suffix):测试此字符串是否以指定的后缀结束
boolean isd = str1.endsWith("d");
System.out.println("此字符串是否以d结尾:"+isd);
boolean isf = str1.endsWith("f");
System.out.println("此字符串是否以f结尾:"+isf);
运行结果:
此字符串是否以d结尾:true
此字符串是否以f结尾:false
而与endsWith(String suffix)类似的方法有下面两个:
startsWith(String prefix)
startsWith(String prefix, int toffset)
这两个的方法是测试此字符串是否以指定的后缀开始
12. equals(Object anObject):将此字符串与指定的对象比较
boolean iseq1 = str1.equals("abcd");
System.out.println(iseq1);
boolean iseq2 = str1.equals("abc");
System.out.println(iseq2);
运行结果:
true
false
13. equalsIgnoreCase(String anotherString):将此 String 与另一个 String 比较,不考虑大小写
boolean iseq1 = str1.equalsIgnoreCase("ABCD");
System.out.println(iseq1);
运行结果:
true
14. getBytes():将字符串转化为字节数组
byte[] data = str1.getBytes();
for (int i=0;i
}