String类


1. 求字符串长度
public int length()


2. 求字符串某一位置字符
//返回字符串中指定位置的字符;注意字符串中第一个字符索引是0,最后一个是length()-1。
public char charAt(int index)


3. 提取子串
//该方法从beginIndex位置起,从当前字符串中取出剩余的字符作为一个新的字符串返回。
public String substring(int beginIndex)

//该方法从beginIndex位置起,从当前字符串中取出到endIndex-1位置的字符作为一个新的字符串返回。
public String substring(int beginIndex, int endIndex)


4. 字符串比较
//该方法是对字符串内容按字典顺序进行大小比较,通过返回的整数值指明当前字符串与参数字符串的大小关系。若当前对象比参数大则返回正整数,反之返回负整数,相等返回0。
public int compareTo(String anotherString)

//与compareTo方法相似,但忽略大小写。
public int compareToIgnore(String anotherString)

//比较当前字符串和参数字符串,在两个字符串相等的时候返回true,否则返回false。
public boolean equals(Object anotherObject)

//与equals方法相似,但忽略大小写。
public boolean equalsIgnoreCase(String anotherString)


5. 字符串连接
//将参数中的字符串str连接到当前字符串的后面,效果等价于"+"。
public String concat(String str)


6. 返回字符串位置
//用于查找当前字符串中字符或子串,返回字符或子串在当前字符串中从左边起首次出现的位置,若没有出现则返回-1。
public int indexOf(int ch/String str)

//改方法与第一种类似,区别在于该方法从fromIndex位置向后查找。
public int indexOf(int ch/String str, int fromIndex)

//该方法与第一种类似,区别在于该方法从字符串的末尾位置向前查找。
public int lastIndexOf(int ch/String str)

//该方法与第二种方法类似,区别于该方法从fromIndex位置向前查找。
public int lastIndexOf(int ch/String str, int fromIndex)


7. 字符串中字符的大小写转换
//返回将当前字符串中所有字符转换成小写后的新串
public String toLowerCase()

//返回将当前字符串中所有字符转换成大写后的新串
public String toUpperCase()


8. 字符串中字符的替换
//用字符newChar替换当前字符串中所有的oldChar字符,并返回一个新的字符串。
public String replace(char oldChar, char newChar)

//该方法用字符replacement的内容替换当前字符串中遇到的第一个和字符串regex相匹配的子串,应将新的字符串返回。
public String replaceFirst(String regex, String replacement)

//该方法用字符replacement的内容替换当前字符串中遇到的所有和字符串regex相匹配的子串,应将新的字符串返回。
public String replaceAll(String regex, String replacement)


9. 将此字符串按照给定的regex(规则)拆分为字符串数组。
public String[] split(String regex)


10. 去掉字符串左右空格
trim();


11. 判断一个字符串里面是否包含指定的内容,返回一个布尔值
boolean contains(String)


12. 测试此字符串是否以指定的前缀开始。返回一个布尔值
boolean startsWith(String)  


13. 测试此字符串是否以指定的后缀结束。返回一个布尔值
boolean endsWith(String)


原文地址:https://www.cnblogs.com/ngy-liupeng/p/15246523.html