Bootstrap

【String 类 常用方法详解和归类】全网最细总结

一、 String 介绍

  • String 类代表的字符串,程序中任何字符串都是String类的对象;

  • 字符串是常量;它们的值在创建之后不能更改;

  • String类提供了很多操作字符串的方法,查找,判断,截取,替换,转换等等…

二、String 类中查找字符串的方法

  • 获取字符串长度
  • int length()

2.1 常用查找

char charAt(int index)
------返回指定索引处的 char 值。


在这里插入图片描述


int codePointAt(int index)
-----返回指定索引处的字符(Unicode 代码点)


在这里插入图片描述


int codePointBefore(int index)
---- 返回指定索引之前的字符(Unicode 代码点)。


在这里插入图片描述


int indexOf(int ch)
----返回指定字符在此字符串中第一次出现处的索引。


在这里插入图片描述


int indexOf(int ch, int fromIndex)
-----返回在此字符串中第一次出现指定字符处的索引,从指定的索引开始搜索。


在这里插入图片描述


int indexOf(String str)
-----返回指定子字符串在此字符串中第一次出现处的索引。


在这里插入图片描述


int indexOf(String str, int fromIndex)
-----返回指定子字符串在此字符串中第一次出现处的索引,从指定的索引开始。


在这里插入图片描述

2.2、其他查找

返回值类型方法名方法描述
static StringcopyValueOf(char[] data)返回指定数组中表示该字符序列的 String
static StringcopyValueOf(char[] data, int offset, int count)返回指定数组中表示该字符序列的 String
intlastIndexOf(int ch)返回指定字符在此字符串中最后一次出现处的索引
int lastIndexOf(int ch, int fromIndex)返回指定字符在此字符串中最后一次出现处的索引,从指定的索引处开始进行反向搜索
intlastIndexOf(String str)返回指定子字符串在此字符串中最右边出现处的索引
intlastIndexOf(String str, int fromIndex)返回指定子字符串在此字符串中最后一次出现处的索引,从指定的索引开始反向搜索

三、转换功能

3.1 常用转换方法

static String valueOf(任意类型)
将任意类型转成字符串

String s = String.valueOf(1);
System.out.println("s = " + s);
System.out.println(s + 1 );//11

byte[] getBytes()
---------使用平台的默认字符集将此 String 编码为 byte 序列,并将结果存储到一个新的 byte 数组中。

byte[] bytes = "abc".getBytes( );
System.out.println(Arrays.toString(bytes));//[97, 98, 99]

char[] toCharArray()
将此字符串转换为一个新的字符数组。

char[] charArray = "abc".toCharArray( );
System.out.println(Arrays.toString(charArray));//[a, b, c]

String toLowerCase()
使用默认语言环境的规则将此 String 中的所有字符都转换为小写。

  System.out.println("ABC".toLowerCase( ));//abc

String toUpperCase()
使用默认语言环境的规则将此 String 中的所有字符都转换为大写。

  System.out.println("abc".toUpperCase( ));//ABC

3.2、其他转换方法

返回值类型方法名方法描述
byte[]getBytes(Charset charset)使用给定的 charset 将此 String 编码到 byte 序列,并将结果存储到新的 byte 数组
byte[]getBytes(String charsetName)使用指定的字符集将此 String 编码为 byte 序列,并将结果存储到一个新的 byte 数组中
StringtoLowerCase(Locale locale)使用给定 Locale 的规则将此 String 中的所有字符都转换为小写
StringtoUpperCase(Locale locale)使用给定 Locale 的规则将此 String 中的所有字符都转换为大写

四、判断、比较相关方法

4.1、常用判断、比较方法

boolean contains(CharSequence s)
当且仅当此字符串包含指定的 char 值序列时,返回 true。

 System.out.println("abc".contains("1"));//false
 System.out.println("abc".contains("a"));//true

int compareTo(String anotherString)
按字典顺序比较两个字符串。

 System.out.println("abc".compareTo("acb"));//-1
 System.out.println("acb".compareTo("abc"));//1

boolean contentEquals(CharSequence cs)
将此字符串与指定的 CharSequence 比较。

System.out.println("abc".contentEquals("11"));//false
System.out.println("abc".contentEquals("abc"));//true

boolean endsWith(String suffix)
测试此字符串是否以指定的后缀结束。

 System.out.println("abc".endsWith("c"));//true
 System.out.println("abc".endsWith("1"));//false

boolean equals(Object anObject)
将此字符串与指定的对象比较。

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

boolean equalsIgnoreCase(String anotherString)
将此 String 与另一个 String 比较,不考虑大小写。

 System.out.println("abc".equalsIgnoreCase("ABC"));//true

4.2、其他判断、比较方法

返回值类型方法名方法描述
intcompareToIgnoreCase(String str)按字典顺序比较两个字符串,不考虑大小写
booleancontentEquals(StringBuffer sb)将此字符串与指定的 StringBuffer 比较。
booleanmatches(String regex)告知此字符串是否匹配给定的正则表达式。
booleanregionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len)测试两个字符串区域是否相等。

五、拆分,截取,替换方法

5.1、常用拆分,截取,替换方法

String concat(String str)
将指定字符串连接到此字符串的结尾。

        System.out.println("abc".concat("d"));//abcd

void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
将字符从此字符串复制到目标字符数组。

 char[] chars = {'c','c','c','d'};
 "abc".getChars(0,2,chars,0);
System.out.println(Arrays.toString(chars));//[a, b, c, d]

String replace(char oldChar, char newChar)
返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所有 oldChar 得到的。

 System.out.println("abc".replace('a', 'c'));//cbc

String[] split(String regex)
根据给定正则表达式的匹配拆分此字符串。

String[] strings = "java-API".split("-");
System.out.println(Arrays.toString(strings));//[java, API]

String substring(int beginIndex)
返回一个新的字符串,它是此字符串的一个子字符串。

 System.out.println("abcd".substring(1));//bcd

String substring(int beginIndex, int endIndex)
返回一个新字符串,它是此字符串的一个子字符串。

System.out.println("abcd".substring(1,3));//bc

5.2、其他拆分,截取,替换方法

返回值类型方法名方法描述
Stringreplace(CharSequence target, CharSequence replacement)使用指定的字面值替换序列替换此字符串所有匹配字面值目标序列的子字符串
StringreplaceAll(String regex, String replacement)使用给定的 replacement 替换此字符串所有匹配给定的正则表达式的子字符串
StringreplaceFirst(String regex, String replacement)使用给定的 replacement 替换此字符串匹配给定的正则表达式的第一个子字符串
String[]split(String regex, int limit)根据匹配给定的正则表达式来拆分此字符串。

最后

编写不易,如果感觉有收获的话,点个赞 👍🏻 吧。
❤️❤️❤️本人菜鸟修行期,如有错误,欢迎各位大佬评论批评指正!😄😄😄

💘💘💘如果觉得这篇文对你有帮助的话,也请给个点赞、收藏下吧,非常感谢!👍 👍 👍在这里插入图片描述

;