Java String类方法总结
charAt
- public char charAt(int index)
返回指定索引处的 char 值。索引范围为从 0 到 length() - 1。序列的第一个 char 值位于索引 0 处,第二个位于索引 1 处,依此类推,这类似于数组索引。
getChars
- public void getChars(int srcBegin, int srcEnd,char[] dst,int dstBegin)
将字符从此字符串复制到目标字符数组
参数:
srcBegin - 字符串中要复制的第一个字符的索引。
srcEnd - 字符串中要复制的最后一个字符之后的索引。
dst - 目标数组。
dstBegin - 目标数组中的起始偏移量
equals
- public boolean equals(Object anObject)
将此字符串与指定的对象比较。当且仅当该参数不为 null,并且是与此对象表示相同字符序列的 String 对象时,结果才为 true。
equalsIgnoreCase
- 将此 String 与另一个 String 比较,不考虑大小写。如果两个字符串的长度相同,并且其中的相应字符都相等(忽略大小写),则认为这两个字符串是相等的。
在忽略大小写的情况下,如果下列至少一项为 true,则认为 c1 和 c2 这两个字符相同。
1.这两个字符相同(使用 == 运算符进行比较)。
2.对每个字符应用Character.toUpperCase(char) 生成相同的结果。
3.对每个字符应用Character.toLowerCase(char) 生成相同的结果。
compareTo
compareToIgnoreCase
concat
"cares".concat("s") returns "caress"
"to".concat("get").concat("her") returns "together"
replace
- public String replace(char oldChar,char newChar)
这个是只针对单个字符的
返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所有 oldChar 得到的。
示例:
"mesquite in your cellar".replace('e', 'o')
returns "mosquito in your collar"
"the war of baronets".replace('r', 'y')
returns "the way of bayonets"
"sparring with a purple porpoise".replace('p', 't')
returns "starring with a turtle tortoise"
"JonL".replace('q', 'x') returns "JonL" (no change)
replaceAll
- String s11=”我喜欢你,江南,非常喜欢”;
System.out.println(s11.replaceAll(“喜欢”,”讨厌”));
toLowerCase
- 使用默认语言环境的规则将此 String 中的所有字符都转换为小写。
toUpperCase
- 使用默认语言环境的规则将此 String 中的所有字符都转换为大写。
trim
toCharArray
indexOf
- public int indexOf(int ch)
返回指定字符在此字符串中第一次出现处的索引。
public int indexOf(String str)
返回指定子字符串在此字符串中第一次出现处的索引。
public int indexOf(int ch, int fromIndex)
返回在此字符串中第一次出现指定字符处的索引,从指定的索引开始搜索。
public int indexOf(String str, int fromIndex)
返回指定子字符串在此字符串中第一次出现处的索引,从指定的索引开始。
还有一个lastIndexOf正好全部和indexOf相反
isEmpty()
- 当且仅当 length() 为 0 时返回 true。
length()
substring(int beginIndex)