建议收藏,花了一个星期整理的String类的方法总结,讲的非常详细

225 阅读5分钟

一、Constructor Summary/构造函数的总结

① public String()

初始化新创建的String对象,以使其表示空字符序列。 请注意,使用此构造函数是不必要的,因为Strings是不可变的。

创建字符串对象最常用的一种方式。

String s1 = "hello world!";

② public String(byte[] bytes)

通过使用平台的默认字符集解码指定的字节数组构造了一个新的 String。

​​​把byte数组全部转为字符串。

byte[] bytes = {97, 98, 99};//在ASCII码中97/a 98/b 99/c
String s2 = new String(bytes);
System.out.println(s2);//abc

③ public String(byte[] bytes, int offset, int length)

通过使用平台的默认字符集解码指定的字节子阵列来构造新的String 。 把byte数组中的一部分转为字符串 byte[] bytes:字节数组 int offset:元素下标的起始位置  int length:长度

byte[] bytes = {97, 98, 99};//在ASCII码中97/a 98/b 99/c
String s3 = new String(bytes,1,2);
System.out.println(s3);//bc

④ public String(char[] value)

将char数组全部转为字符串

char[] chars = {'我','是','友','人','A'};
String s4 = new String(chars);
System.out.println(s4);//我是友人A

⑤ public String(char[] value, int offset, int count)

将char数组部分转为字符串

String s6 = new String("哈哈哈");

⑥ public String(String original)

初始化新创建的String对象,使其表示与参数相同的字符序列; 换句话说,新创建的字符串是参数字符串的副本。

String s6 = new String("哈哈哈");

二、Method Summary/方法的总结

1. public char charAt(int index)

返回指定索引的 char的值。 指数范围从 0到 length() - 1。 第一序列的 char值在指数 0,在指数 1下,等等,作为数组索引。 char c = "我是友人A".charAt(3);

char c = "我是友人A".charAt(3);
System.out.println(c);//人

2. public char[] toCharArray()

将此字符串转换为新的字符数组。

char[] chars = "我是友人A".toCharArray();
for (int i = 0; i <chars.length ; i++) {
    System.out.print(chars[i] +" ");
}
/*
我 是 友 人 A
*/

3. public int compareTo(String anotherString)

比较两个字符串的字典。 比较是基于字符串中各个字符的Unicode值。

int result = "abc".compareTo("abc");
int result2 = "abce".compareTo("abcd");
int result3 = "abcd".compareTo("abce");
int result4 = "abcd".compareTo("bcad");
System.out.println(result);//0(等于0) 前后一致 
System.out.println(result2);//1(大于0) 前大后小 
System.out.println(result3);//-1(小于0) 前小后大 
System.out.println(result4);//-1(小于0) 这里比第一个就够了,后面字母不比。能分胜负就不比了

4. public boolean contains(CharSequence s)

如果并且只有当此字符串包含指定的字符序列的字符串值,则返回真值。

System.out.println("友人A".contains("友五"));//false
System.out.println("友人A".contains("友人"));//true

5. public boolean endsWith(String suffix)

判断字符创是否以某个字符串结尾。

System.out.println("test.txt".endsWith(".java"));//false
System.out.println("test.txt".endsWith(".txt"));//true

6. public boolean startsWith(String prefix,int toffset)

判断某个字符串以某个字符串开始

System.out.println("qwwwqwqfdf".startsWith("qw"));//true

7. public boolean equals(Object anObject)

比较两个字符串必须使用equals方法,不能使用“==” equals()方法有没有调用compareTo()方法? 老版本equals()方法底层调用compareTo()方法 新版本jdk是底层是有数组实现的比较

System.out.println("abc".equals("abc"));//true

8. public boolean equalsIgnoreCase(String anotherString)

判断两个字符串是否相等,忽略大小写

System.out.println("abc".equals("Abc"));//false
System.out.println("abc".equalsIgnoreCase("ABc"));//true

9. public byte[] getBytes()

将字符串对象转为字节数组

byte[] bb = "abc".getBytes();
for (int i = 0; i <bb.length ; i++) {
    System.out.print(bb[i]+" ");
}
//97 98 99

10. public int indexOf(String str)

返回某个字符串在当前字符串中第一次出现的索引。

System.out.println("addppqkufppq".indexOf("ppq"));//3

11. public int lastIndexOf(int ch)

返回某个字符串在当前字符串中最后一次出现的索引。

System.out.println("addppqkufppq".lastIndexOf("ppq"));//9

12. public boolean isEmpty()

判断一个字符串是否为空字符串,底层源代码调用的是数组的length属性

String s = "";
System.out.println(s.isEmpty());//true
System.out.println(s.length());//true

13. public int length()

返回此字符串的长度。 长度等于字符串中的 Unicode code units数。 数组的length属性,数组元素个数

System.out.println("78".length());//2
 
int[] a = {78,88,99};
String s4 = Arrays.toString(a);
System.out.println(s4.length());//12
System.out.println(a.length);//3

14. public String replace(CharSequence target,CharSequence replacement)

Parameters:

CharSequence:String的父接口 target -要被替换的字符 replacement -替换的字符 Returns: 结果字符串

String Returns = "youren@A".replace("@A","@B");
System.out.println("替换:"+Returns);//youren@B

15. public String[] split(String regex)

拆分字符串 返回一个字符串数组

String[] ymd = "1980-10-11".split("-");//以“-”分隔符进行拆分
for (int i = 0; i <ymd.length ; i++) {
    System.out.println(ymd[i]);
}
/*
1980
10
11
*/

16.public String substring(int beginIndex)

截取字符串,截取beginIndex下标后面的字符串 public String substring(int beginIndex,int endIndex)

截取开始到结束

System.out.println("hhheeefff".substring(3));//eeefff
System.out.println("hhheeefff".substring(0,5));//左闭右开[beginIndex,endIndex) hhhee

17.public String toLowerCase()

转换成小写 public String toUpperCase(Locale locale)

转成大写

System.out.println("ABCDE".toLowerCase());//abcd
System.out.println("abcd".toUpperCase());//ABCDE

18. public String trim()

返回一个字符串,其值为此字符串,并删除任何前导和尾随空格。去除前后空白

System.out.println("     hello world       ".trim());//hello world

通过源代码可以看出:为什么输出一个引用的时候,会调用toString()方法

因为System.out.println()方法,底层调用的是toString()方法 能够输出到控制台都是字符串 valueOf():把不是字符串的转为字符串 toString():valueOf()方法底层调用的toString()方法返回字符串 输出一个引用的时候,会把引用转换为字符串,转换时用valueOf()方法,valueOf()方法调用的对象的toString()方法,具体要看子类有没有重写Object类的toString()方法,没有重写,输出的就是地址。 字符串输出到控制台 //本质上System.out.println()在输出任何数据的时候,都是先转换为字符串在输出。

最后

感谢你看到这里,看完有什么的不懂的可以在评论区问我,觉得文章对你有帮助的话记得给我点个赞,每天都会分享java相关技术文章或行业资讯,欢迎大家关注和转发文章!