Java String的常用方法

来源:互联网 发布:mac os 使用教程 编辑:程序博客网 时间:2024/06/06 07:24

String的常用方法及注意事项

public char charAt(int index)

返回指定索引处的字符。

public int compareTo(String anotherString)
public int compareToIgnoreCase(String str)

比较两个字符串的字典顺序。比较两个字符串按字典顺序,不区分大小写的差异。1) 相等关系返回0。2) 不相等时,从两个字符串第0个字符开始比较,返回第一个不相等的字符差:this.charAt(k)-anotherString.charAt(k)。3) 较长字符串的前面部分恰巧是较短的字符串,返回它们的长度差:this.length()-anotherString.length()。

public String concat(String str)

将指定的字符串串连到这个字符串的结尾。

public boolean contains(CharSequence s)

返回true当且仅当此字符串包含char值的特定序列。

public boolean endsWith(String suffix)

测试此字符串是否以suffix后缀结束。

public boolean equals(Object anObject)

当anObject不为空并且与当前String对象一样,返回true;否则,返回false。

public boolean equalsIgnoreCase(String anotherString)

比较该字符串与anotherString,不考虑大小写。

public byte[] getBytes()

将该String对象转换成byte数组。

public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)

复制字符从该字符串到目标字符数组。srcBegin为拷贝的起始位置,srcEnd为拷贝的结束位置,dst为目标字符数组,dstBegin为目标字符数组的拷贝起始位置。

public int hashCode()

返回此字符串的哈希码。

public int indexOf(int ch)
public int indexOf(int ch, int fromIndex)

返回此字符串指定字符第一次出现处的索引。

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

返回此字符串指定子字符串第一次出现处的索引。

public String intern()

返回字符串对象的规范表示。

public boolean isEmpty()

返回true当且仅当此字符串长度为0。

public int lastIndexOf(int ch)
public int lastIndexOf(int ch, int fromIndex)

返回此字符串指定字符最后一次出现处的索引。

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

返回此字符串指定子字符串最后一次出现处的索引。

public int length()

返回此字符串的长度。

public boolean matches(String regex)

此字符串是否给定的正则表达式匹配。当匹配正则表达式的特殊字符时,需加"\\"转义。* . ? + $ ^ [ ] ( ) { } | \ /

public String replace(char oldChar, char newChar)

返回此字符串中使用newChar替换oldChar产生的一个新的字符串。

public String replace(CharSequence target, CharSequence replacement)

返回此字符串中使用replacement替换CharSequence产生的一个新的字符串。

public String replaceAll(String regex, String replacement)

使用replacement替换此字符串匹配给定的正则表达式的每个子字符串。Pattern.compile(regex).matcher(str).replaceAll(repl)

public String replaceFirst(String regex, String replacement)

使用replacement替换此字符串中第一个匹配给定的正则表达式的子字符串。Pattern.compile(regex).matcher(str).replaceFirst(repl)

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

分割此字符串围绕匹配给定的正则表达式。当匹配正则表达式的特殊字符时,需加"\\"转义。* . ? + $ ^ [ ] ( ) { } | \ /

public boolean startsWith(String prefix)
public boolean startsWith(String prefix, int toffset)

测试此字符串是否以prefix前缀开始。

public CharSequence subSequence(int beginIndex, int endIndex)

返回一个新的字符序列,它是此序列的子序列。

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

返回一个新字符串,它是此字符串的一个子字符串。新字符串的Length为:endIndex - beginIndex。

public char[] toCharArray()

将此字符串转换为一个新的字符数组。

public String toLowerCase()
public String toLowerCase(Locale locale)

将此字符串中的所有字符使用默认语言环境的规则小写。

public String toString()

该对象本身返回。

public String toUpperCase()
public String toUpperCase(Locale locale)

将此字符串中的所有字符使用默认语言环境的规则大写。

public String trim()

返回字符串的一个副本,忽略字符串开头和结尾的空白(空格)。

public static String valueOf(T t)

将参数转为字符串。
0 0