JAVA中类String常用方法整理

Class String

public final class String extends Object 常用的方法小结: 1.public char charAt(int index) 将索引index处的字符返回。例,"hello".charAt(2);则返回'='l'。 2. public boolean equals(Object anObject) 将此字符串与指定对象进行比较。 其结果是true当且仅当该参数不是null并且是String对象,表示相同的字符序列作为该对象。 3. public boolean equalsIgnoreCase(String anotherString) 两个字符串内容对比,忽略大小写,如果相同,则返回true,不同,则返回false。例,boolean b=str1.equalsIgnoreCase(str2); 4. public boolean contentEquals(StringBuffer sb) String类型的字符串与StringBuffer类型相比,内容相同,返回true,不同,返回false.例,boolean a=str.contenEquals(sb); 5. public boolean startsWith(String str,int index) 某一字符串在索引index处,是否以str开始,如果是,返回true,不是,返回false.例,boolean a=str1.startsWith("bc",0); 6. public boolean endsWith(String str) 某一字符串是否以str结尾,是则返回true,不是,则返回false。 7. public int indexOf(int x/String str) 返回整型x或字符串str在某一字符串中第一次的索引。例,int a =str.indexOf(4); int a=str.indexOf("bc");若没有此值或字符串,返回-1; 8. public int indexOf(int x,int fromIndex) 与上面的方法类似,只是从fromIndex处开始进行搜索。 9.public int indexOf(int x/String str) 和public int indexOf(int x,int fromIndex)与7、8方法相似,只是最后一次出现的索引。 10.public String substring(int beginIndex) 从索引beginIndex处截取字符串,并将此字符串返回。例,“hello".substring(2),返回"llo". 11.public String substring(int beginIndex,int endIndex) 从索引beginIndex到endIndex-1截取字符串,并将此字符串返回。例,“hello".substring(1,2),返回"e". 12.public String concat(String str) 将字符串str追加到字符串的末尾。例,"hello".concat("world"),则返回“helloworld". 13.public String replace(char oldChar,char newChar) 将字符串中的字符oldChar,用newChar替换,并返回新的字符串。例,"happy".replace('p','o'),返回"haooy". 14.public String replaceFirst(String regex,String replacement) 用replacement替换第一次出现的regex。例,"heeheehee".replaceFirst("ee","oo"),返回"hooheehee". 15.public String replaceAll(String regex,String replacement) 用replacement替换全部的regex,与13相似,只是这个替换的是String类型。 16.public String[] split(String regex) 用regex来分隔字符串,并返回一个字符串数组。例,"hello world happy".split(" "),返回{"hello","world","happy"} 注意,若字符串是以|或*等分隔的,则regex须是"\\|"或"\\*"。 17.public String toLowerCase()和public String toUpperCase() 将字符串全部转为小写或大写。例,"hello".toUpperCase(),则返回"HELLO"。 18.public String trim() 将字符串的前导和尾部空格删除,然后返回字符串。例,“ hello ".trim();返回"hello"。 19.public Char[] toCharArray() 将字符串转化为字符数组。 20.public static String valueOf(int i) 将整型数i转化为字符串。例,String s=String.valueOf(89);则s为"89". 还可将其他基本类型应用此方法。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值