substring()
它有兩種形式,第一種是:String substring(int startIndex)
第二種是:String substring(int startIndex,int endIndex)
concat() 連接兩個(gè)字符串
replace() 替換
它有兩種形式,第一種形式用一個(gè)字符在調(diào)用字符串中所有出現(xiàn)某個(gè)字符的地方進(jìn)行替換,形式如下:
String replace(char original,char replacement)
例如:String s=”Hello”.replace(’l',’w');
第二種形式是用一個(gè)字符序列替換另一個(gè)字符序列,形式如下:
String replace(CharSequence original,CharSequence replacement)
trim() 去掉起始和結(jié)尾的空格
valueOf() 轉(zhuǎn)換為字符串
toLowerCase() 轉(zhuǎn)換為小寫(xiě)
toUpperCase() 轉(zhuǎn)換為大寫(xiě)
length() 取得字符串的長(zhǎng)度
例:
char chars[]={’a',’b’.’c'};
String s=new String(chars);
int len=s.length();
charAt() 截取一個(gè)字符
例:
char ch;
ch=”abc”.charAt(1);
返回值為’b’
getChars() 截取多個(gè)字符
void getChars(int sourceStart,int sourceEnd,char target[],int targetStart)
sourceStart 指定了子串開(kāi)始字符的下標(biāo)
sourceEnd 指定了子串結(jié)束后的下一個(gè)字符的下標(biāo)。因此,子串包含從sourceStart到sourceEnd-1的字符。
target 指定接收字符的數(shù)組
targetStart target中開(kāi)始復(fù)制子串的下標(biāo)值
例:
String s=”this is a demo of the getChars method.”;
char buf[]=new char[20];
s.getChars(10,14,buf,0);
getBytes()
替代getChars()的一種方法是將字符存儲(chǔ)在字節(jié)數(shù)組中,該方法即getBytes()
例:
String s = “Hello!你好!”;
byte[] bytes = s.getBytes();
toCharArray()
例:
String s = “Hello!你好!”;
char[] ss = s.toCharArray();
equals()和equalsIgnoreCase() 比較兩個(gè)字符串
regionMatches() 用于比較一個(gè)字符串中特定區(qū)域與另一特定區(qū)域,它有一個(gè)重載的形式允許在比較中忽略大小寫(xiě)。
boolean regionMatches(int startIndex,String str2,int
str2StartIndex,int numChars)
boolean regionMatches(boolean ignoreCase,int startIndex,String
str2,int str2StartIndex,int numChars)
startsWith()和endsWith()
startsWith()方法決定是否以特定字符串開(kāi)始,endWith()方法決定是否以特定字符串結(jié)束
equals()和==
equals()方法比較字符串對(duì)象中的字符,==運(yùn)算符比較兩個(gè)對(duì)象是否引用同一實(shí)例。
例:String s1=”Hello”;
String s2=new String(s1);
s1.eauals(s2); //true
s1==s2;//false
compareTo()和compareToIgnoreCase() 比較字符串
indexOf()和lastIndexOf()
indexOf() 查找字符或者子串第一次出現(xiàn)的地方。
lastIndexOf() 查找字符或者子串是后一次出現(xiàn)的地方。
StringBuffer構(gòu)造函數(shù)
StringBuffer定義了三個(gè)構(gòu)造函數(shù):
StringBuffer()
StringBuffer(int size)
StringBuffer(String str)
StringBuffer(CharSequence chars)
下面是StringBuffer相關(guān)的函數(shù):
length()和capacity()
一個(gè)StringBuffer當(dāng)前長(zhǎng)度可通過(guò)length()方法得到,而整個(gè)可分配空間通過(guò)capacity()方法得到。
ensureCapacity() 設(shè)置緩沖區(qū)的大小
void ensureCapacity(int capacity)
setLength() 設(shè)置緩沖區(qū)的長(zhǎng)度
void setLength(int len)
charAt()和setCharAt()
char charAt(int where)
void setCharAt(int where,char ch)
getChars()
void getChars(int sourceStart,int sourceEnd,char target[],int targetStart)
append() 可把任何類(lèi)型數(shù)據(jù)的字符串表示連接到調(diào)用的StringBuffer對(duì)象的末尾。
例:int a=42;
StringBuffer sb=new StringBuffer(40);
String s=sb.append(”a=”).append(a).append(”!”).toString();
insert() 插入字符串
StringBuffer insert(int index,String str)
StringBuffer insert(int index,char ch)
StringBuffer insert(int index,Object obj)
index指定將字符串插入到StringBuffer對(duì)象中的位置的下標(biāo)。
reverse() 顛倒StringBuffer對(duì)象中的字符
StringBuffer reverse()
delete()和deleteCharAt() 刪除字符
StringBuffer delete(int startIndex,int endIndex)
StringBuffer deleteCharAt(int loc)
replace() 替換
StringBuffer replace(int startIndex,int endIndex,String str)
substring() 截取子串
String substring(int startIndex)
String substring(int startIndex,int endIndex)