Java中的String类包含了50多个方法。
下面是一些最常用的方法
java.lang.String
char charAt(int index)
返回给定位置的代码单元。除非对底层的代码单元感兴趣,否则不需要调用这个方法。
int codePointAt(int index)
返回从给定位置开始的码点。
int offsetByCodePoints(int startIndex , int cpCount)
返回从startIndex码点开始,cpcount个码点后的码点索引。
int compareTo(String other)
按照字典顺序,如果字符串位于other之前,返回一个负数;如果字符串位于other之后,返回一个正数;如果两个字符串相等,返回0。
intstream codePoints()
将这个字符串的码点作为一个流返回。调用toArray将它们放在一个数组中。
new String(int[] codePoints , int offset , int count)
用数组中从offset开始的count个码点构造一个字符串。
boolean empty()
boolean blank()
如果字符串为空或者由空格组成,返回true。
boolean equals(Object other)
如果字符串与other相等,返回true。
boolean equalsIgnoreCase(String other)
如果字符串与other相等(忽略大小写),返回true。
boolean startsWith(String prefix)
boolean endsWith(String suffix)
如果字符串以prefix开头或以suffix结尾,则返回true。
int indexOf(String str)
int indexOf(String str , int fromIndex)
int indexOf(int cp)
int indexOf(int cp , int fromIndex)
返回与字符串str或码点cp匹配的第一个子串的开始位置。从索引0或fromIndex开始匹配。如果在原始字符串中不存在str,则返回-1.
int lastIndexOf(String str)
int lastIndexOf(String str , int fromIndex)
int lastIndexOf(int cp)
int lastIndexOf(int cp , int fromIndex)
返回与字符串str或码点cp匹配的最后一个子串的开始位置。从原始字符串末尾或fromIndex开始匹配。
int length()
返回字符串代码单元的个数。
int codePointCount(int startIndex , int endIndex)
返回startIndex和endIndex-1之间的码点个数。
String replace(CharSequence oldString , CharSequence newString)
返回一个新字符串。这个字符串用newString代替原始字符串中的所有的oldString。可以用String或StringBuilder对象作为charSequence参数。
String substring(int beginIndex)
String substring(int beginIndex , int endIndex)
返回一个新字符串。这个字符串包含原始字符串中从beginIndx到字符串末尾或endIndex-1的所有代码单元。
String toLowerCase()
String toUpperCase()
返回一个新字符串。这个字符串将原始字符串中的大写字母改为小写字母,或将原始字符串中的所有小写字母改成大写字母。
String trim()
String strip()
返回一个新字符串。这个字符串将删除原始字符串头部和尾部小于等于U+0020的字符(trim)或空格(strip)。
String ioin(CharSequence delimiter , CharSequence…elements)
返回一个新字符串,用给定的定界符连接所有元素。
String repeat(int count)
返回一个字符串,将当前字符重复count次。
- 注释:在API注释中,有一些CharSequence类型的参数。这是一种接口类型,所有字符串都属于这个接口。