String,StringBuffer,StringBuilder及StringUtils的常用方法

JDK本身是没有针对字符串的工具类,可能他们觉得String三巨头本身自带的方法就够用了,想要String工具类就只能去Apache官网下载StringUtils这个工具类的jar包。

一、String

注意:因为String不可变,所以,一旦方法返回字符串,那一定是一个新建的字符串。

实例方法:

1、求字符串长度
public int length();

2、求字符串某一位置字符
public char charAt(int index)

3、字符串连接
public String concat(String str)               //将参数中的字符串str连接到当前字符串的后面,效果等价于"+"。

4、查找子串位置
public int indexOf(String str)     //用于查找当前字符串中字符或子串,返回字符或子串在当前字符串中从左边起首次出现的位置,若没有出现则返回-1。
public int indexOf(String str, int fromIndex)     //该方法与第一种类似,区别在于该方法从fromIndex位置开始,向后查找。
public int lastIndexOf(String str)         //该方法与第一种类似,区别在于该方法从字符串的末尾位置向前查找。
public int lastIndexOf(String str, int fromIndex)       //该方法与第二种方法类似,区别于该方法从fromIndex位置开始,向前查找。

5、字符串中字符的大小写转换
public String toLowerCase()     //返回将当前字符串中所有字符转换成小写后的新串
public String toUpperCase()     //返回将当前字符串中所有字符转换成大写后的新串

6、字符串中字符的替换
public String replace(char oldChar, char newChar)               //用字符newChar替换当前字符串中所有的oldChar字符,并返回一个新的字符串。
public String replaceFirst(String regex, String replacement)                   //该方法用字符replacement的内容替换当前字符串中遇到的第一个和字符串regex相匹配的子串,应将新的字符串返回。
public String replaceAll(String regex, String replacement)                       //该方法用字符replacement的内容替换当前字符串中遇到的所有和字符串regex相匹配的子串,应将新的字符串返回,其实这个方法完全可以取代replace方法,只不过对字符来讲,用replace能快一点。

7、字符串比较(一个返回int,一个返回boolean)
public int compareTo(String anotherString)                         //该方法是对字符串内容按字典顺序进行大小比较,通过返回的整数值指明当前字符串与参数字符串的大小关系。若当前对象比参数大则返回正整数,反之返回负整数,相等返回0。
public int compareToIgnore(String anotherString )                 //与compareTo方法相似,但忽略大小写。
public boolean equals(Object anotherObject)                             //比较当前字符串和参数字符串,在两个字符串相等的时候返回true,否则返回false。
public boolean equalsIgnoreCase(String anotherString)               //与equals方法相似,但忽略大小写

8、提取子串
用String类的substring方法可以提取字符串中的子串,该方法有两种常用参数:
public String substring(int beginIndex)          //该方法从beginIndex位置起,从当前字符串中取出剩余的字符作为一个新的字符串返回。
public String substring(int beginIndex, int endIndex)                 //该方法从beginIndex位置起,从当前字符串中取出到endIndex-1   位置的字符作为一个新的字符串返回。

9、去除两段空格

String trim()           //截去字符串两端的空格,但对于中间的空格不处理。

10、检验字符串是否以指定前缀开头      或者    以指定后缀结尾

boolean statWith(String prefix)

boolean endWith(String suffix)

10、判断是否包含指定字符串

boolean contains(String str)

11、字符串分解

String[] split(String str)      //将str作为分隔符进行字符串分解,分解后的字字符串在字符串数组中返回。

String str = "asd!qwe|zxc#";
String[] str1 = str.split("!|#");//str1[0] = "asd";str1[1] = "qwe";str1[2] = "zxc";

12、字符串是否为空

boolean isEmpty(String str)   //其实就是判断length()是否为0

 

13、 字符串是否匹配正则表达式

boolean matches(正则 )

https://blog.csdn.net/qq_24505485/article/details/54799882

 

14、将字符串转为字符数组

char[]  toCharArray()

 

15、检测两个字符串在一个区域内是否相等

regionMatches(boolean b, int firstStart, String other, int otherStart, int length)

  • ignoreCase -- 如果为 true,则比较字符时忽略大小写。

  • toffset -- 此字符串中子区域的起始偏移量。

  • other -- 字符串参数。

  • ooffset -- 字符串参数中子区域的起始偏移量。

  • len -- 要比较的字符数。

静态方法:

1、字符串转换为基本类型(注意,这不是字符串的方法,这是基本类型的静态方法)
java.lang包中有Byte、Short、Integer、Float、Double类的调用方法:
public static T parseT(String s)    //T是基本类型

2、进制转换(注意,这不是字符串的方法,这是基本类型的静态方法)
使用Long类中的方法得到整数之间的各种进制转换的方法:
public static byte  Long.toBinaryString(long l)
public static byte  Long.toOctalString(long l)
public static byte  Long.toHexString(long l)
public static byte  Long.toString(long l, int p)//p作为任意进制

3、基本类型转换为字符串类型
String类中提供了String valueOf()放法,用作基本类型转换为字符串类型。
static String valueOf(char data[])
static String valueOf(char data[], int offset, int count)     // 将 char 数组 data 中 由 data[offset] 开始取 count 个元素 转换成字符串 
static String valueOf(T b)      //T是基本类型

4、字符串格式化(这是String的静态方法,但使用有点复杂,需要看文档)

https://blog.csdn.net/cunjue/article/details/51959864

5、 String.Join 方法 (String, String[])
在指定 String 数组的每个元素之间串联指定的分隔符 String,从而产生单个串联的字符串
string [] tmpStr={abc,def,ghi};

string jn = string.Join("-", tmpStr);

此时jn="abc-def-ghi";

 

总结:长度,拼接,通过下标找字符,查找子串起始位置,大小写,替换,截取,全比较,局部比较,去两边空格,匹配开头结尾,转字符数组,匹配正则,为空,包含子串,忽略大小写,提取子串,格式化,字符串和基本类型相互转化、串联

20个

 

 

二、StringBuilder,StringBuffer

StringBuilder 和 StringBuffer 方法和功能完全一致只是一个是早期版本(StringBuffer)是线程安全的,由于发现利用多线程堆同一String数据操作的情况是很少的,为了提高效率idk1.5以后有StringBuilder 类。意思是多线程操作同一字符串的时候用StringBuffer 安全,现在一般用StringBuilder

 StringBuffer类中的方法主要偏重于对于字符串的变化,例如追加、插入和删除等,这个也是StringBuffer和String类的主要区别。

注意:这和String不一样,StringBuffer是可变的,返回的还是原来的实例

1、追加字符串到Stringbuffer

StringBuffer append(T t)  //这是Stringbuffer最神奇的地方,你参数不管是啥都转成字符,给你塞到stringbuffer后边

StringBuffer append(char[] str,int offset, int len)   //追加一个指定开始位置和长度的字符数组

StringBuffer insert(int offset,T t)   //插入和追加差不多,要注意的是在offset位置前插入

StringBuffer insert(int index, char[] str, int offset, int len)

 

注意:如果我们想把基本类型数组变成StringBuffer,我们应该for循环把元素append进Stringbuffer。

2、求StringBuffer长度

int capacity()和int length()是不同的,一个返回StringBuffer这个动态数组的长度,数组长度很可能大于等于字符数量;一个是返回其中包含的字符的数量

3、删除StringBuffer中的子串或字符

StringBuffer delete(int   start,int  end)    //删除start下标到end-1下标的所有字符

StringBuffer deleteCharAt(int   index )     //删除指定下标的字符

4、StringBuffer反转

StringBuffer reverse( )

5、修改替换StringBuffer中的字符或子串

void setCharAt(int   index,char ch )   

void replace(int  start,int end,String str )

6、查找子串位置(和String差不多)

public int indexOf(String str)     
public int indexOf(String str, int fromIndex)     
public int lastIndexOf(String str)       
public int lastIndexOf(String str, int fromIndex)  

7、提取子串(和String差不多)
public String substring(int beginIndex)
public String substring(int beginIndex, int endIndex)  

8、压缩空间

public void trimToSize()      //该方法的作用是将StringBuffer对象的中存储空间缩小到和字符串长度一样的长度,减少空间的浪费。

总结:追加,插入,删除,反转,替换,压缩空间,提取子串,查找子串起始位置,长度。  

9个

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值