String类的常用方法总结

来源:互联网 发布:当程序员累不累 编辑:程序博客网 时间:2024/05/21 09:52

String是个常用的类,API中有许多方法,可以方便的操作字符串对象

其实最好应该直接查询Java-doc,而且最好直接查询英文版本的。因为Java是一门优秀的语言,其说明文档非常完备。

1,字符数组与字符串:

字符串变为字符数组:

public char[] toCharArray()

字符数组变为字符串:使用String的构造方法实现

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

2,从字符串中,取出指定位置的字符:

public char charAt(int index)

3,字符串与byte数组的转换:byte数组就是字节数组,在io流操作中经常需要使用到byte数组

字符串转换为字节数组:

public byte[] getBytes()

字节数组变为字符串:

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

4,取得字符串的长度:

public int length()

5,查找制定的字符串是否存在;

public int indexOf(int ch)the index of the first occurrence of the character in the character sequence represented by this object, or -1 if the character does not occur.public int indexOf(int ch, int fromIndex)the index of the first occurrence of the character in the character sequence represented by this object that is greater than or equal to fromIndex, or -1 if the character does not occur.public int lastIndexOf(int ch)the index of the last occurrence of the character in the character sequence represented by this object, or -1 if the character does not occur.public int lastIndexOf(int ch,int fromIndex)the index of the last occurrence of the character in the character sequence represented by this object that is less than or equal to fromIndex, or -1 if the character does not occur before that point.

6,去掉字符串左右两端的空格:

public String trim()

7,字符截取,从一个字符串中取出里面的部分内容:

public String substring(int beginIndex)public String substring(int beginIndex, int endIndex)

8,拆分字符串,这个方法,需要正则支持

public String[] split(String regex)public String[] split(String regex,int limit)

9,大小写转换,

字符串中的全部大写字母变为小写字母:

public String toLowerCase()

字符串中的全部小写字母变为大学字母:

public String toUpperCase()

10,判断是否以指定的字符串开头或者结尾

判断是否以制定字符串开头:

public boolean startsWith(String prefix)

判断是否以制定字符串结尾:

public boolean endsWith(String suffix)

11,字符串相等的判断:

区分大小写的字符串相等比较

public boolean equals(Object anObject)

不区分大小写的字符串相等比较

public boolean equalsIgnoreCase(String anotherString)

12,字符串替换,这个方法需要正则支持

public String replace(char oldChar,char newChar)a string derived from this string by replacing every occurrence of oldChar with newChar.public String replace(CharSequence target,                      CharSequence replacement)Replaces each substring of this string that matches the literal target sequence with the specified literal replacement sequence. The replacement proceeds from the beginning of the string to the end, for example, replacing "aa" with "b" in the string "aaa" will result in "ba" rather than "ab".public String replaceAll(String regex,                         String replacement)Replaces each substring of this string that matches the given regular expression with the given replacement.public String replaceFirst(String regex,                           String replacement)Replaces the first substring of this string that matches the given regular expression with the given replacement.

还是查看API文档靠谱,省时省力,而且是第一手资料。

0 0
原创粉丝点击