String常用方法汇总


1--->public char charAt(int index);返回下标所在的字符


2--->public boolean endsWith(String suffix);字符串是否以suffix字符串结束


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


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


5--->public int lastIndexOf(String str)返回指定子字符串在此字符串中最右边出现处的索引


6--->public int lastIndexOf(String str,int fromIndex)返回指定子字符串在此字符串中最后一次出现处的索引,从指定的索引开始反向搜索


7--->public int length()返回此字符串的长度。


8--->public boolean matches(String regex)告知此字符串是否匹配给定的正则表达式。 


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


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


11-->public String substring(int beginIndex)返回一个新的字符串,它是此字符串的一个子字符串。该子字符串从指定索引处的字符开始,直到此字符串末尾。


12-->public String substring(int beginIndex,int endIndex)返回一个新字符串,它是此字符串的一个子字符串。该子字符串从指定的 beginIndex 处开始,直到索引 endIndex - 1 处的字符。


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


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


15-->public String trim()返回字符串的副本,忽略前导空白和尾部空白。 




StringBuffer:


1--->public StringBuffer append(Object obj);添加到字符串末尾



贴士:

(1):

String是不可变数据:

str = "code";

str = str+"new";

这里str貌似是改变了,但其实是放弃原来str的空间,重新申请一个新的字符串内存空间


(2):

StringBuffer是可以改变的字符串

 

 

单个字符串赋值:public void setCharAt(int index,char ch)

 

指定位置插入字符串:public StringBuffer insert(int offset,String str)

 

倒置字符串的内容:public StringBuffer reverse()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值