String类的常用方法
a、求长度:
int length();
b、比较:
boolean equals
(
Object
anObject)
int compareTo
(
String
anotherString);
// 按字典顺序比较两个字符串。
int compareToIgnoreCase(String str); //不考虑大小写。
boolean contentEquals(CharSequence cs)将此字符串与指定的CharSequence比较。
boolean contentEquals(StringBuffer sb)将此字符串与指定的StringBufferr比较。
boolean regionMatches(boolean ignoreCase,int toffset,String other,int ooffset,int len)测试两个字符串区域是否相等。
boolean regionMatches(int toffset,String other,int ooffset,int len)测试两个字符串区域是否相等。
c、判断:
boolean startsWith(String prefix)测试此字符串是否以指定的前缀开始。
boolean startsWith(String prefix,int toffset)测试此字符串从指定索引开始的子字符串是否以指定的前缀开始。
boolean endsWith(String suffix) 测试此字符串是否以指定的后缀结束。
d、检索
char charAt(int index );
int indexOf(int ch);返回指定字符在此字符串中第一次出现处的索引。????
int indexOf(int ch, int fromIndex) 返回此字符串中第一次出现指定字符处的索引,从指定的索引开始搜索。
int lastIndexOf(int ch) 返回指定字符在此字符串中最后一次出现处的索引,从指定的索引处开始进行反向搜索。
int lastIndexOf(int ch,int fromIndex) 返回指定字符在此字符串中最后一次出现处的索引,从指定的索引处开始进行反向搜索。
int lastIndexOf(String str) 返回指定子字符串在此字符串中最右边出现处的索引。
int lastIndexOf(String str,int fromIndex)返回指定子字符串在此字符串中最后一次出现处的索引,从指定的索引开始反向搜索。
e、求子串:
String substring(int beginIndex) 返回一个新字符串,它是此字符串的一个子字符串。
String substring(int beginIndex,int endIndex) 返回一个新字符串,它是此字符串的一个子字符串。
f、修改:
String replace(char oldChar,char newChar)返回一个新的字符串,它是通过用newChar替换此字符串中出现的掺oldChar得到的。
g、类型转换:
字符串转换成数组类型:
byte[] getBytes();
char[] toCharArray();
将其它类型转换成字符串:
构造方法:
String(byte[] bytes);
String(char[] value);
String(char[] value,int offset,int count);
String(StringBuffer buffer);
常用方法:
h、字符串连接:
String concat(String str) 将指定字符串连接到此字符串的结尾。