1.4、 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的字符。接收字符的數(shù)組由target指定,target中開(kāi)始復(fù)制子串的下標(biāo)值是targetStart。
String s="thisontte";
char[] buf=new char[5];
s.getChars(1, 6, buf, 0);
String temp = "";
for(int i=0;i<buf.length;i++){
temp = temp+buf[i];
}
System.out.print(temp);===hison
1.5、getBytes()
替代getChars()的一種方法是將字符存儲(chǔ)在字節(jié)數(shù)組中,該方法即getBytes()。
String s="thisontte";
byte [] sw = s.getBytes();
System.out.println(sw.length);==9
1.6、toCharArray()
String s="thisontte";
char [] sc = s.toCharArray();
System.out.println(sc.length);==9
1.7、equals()和equalsIgnoreCase() 比較兩個(gè)字符串
String s="abc";
String s1 = "Abc";
System.out.println(s1.equals(s));=== false
System.out.println(s.equalsIgnoreCase(s1));===true
1.8、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)
1.9、startsWith()和endsWith()
startsWith()方法決定是否以特定字符串開(kāi)始,
endWith()方法決定是否以特定字符串結(jié)束
返回boolean值
1.10、equals()和==
equals()方法比較字符串對(duì)象中的字符,==運(yùn)算符比較兩個(gè)對(duì)象是否引用同一實(shí)例。
例:String s1="Hello";
String s2=new String(s1);
s1.eauals(s2); //true
s1==s2;//false
String s="abc";
String s1 = "abc";
System.out.println(s==s1)//true;
1.11、compareTo()和compareToIgnoreCase() 比較字符串
1.12、indexOf()和lastIndexOf()
indexOf() 查找字符或者子串第一次出現(xiàn)的地方。
lastIndexOf() 查找字符或者子串是后一次出現(xiàn)的地方。
1.13、substring()
它有兩種形式,第一種是:String substring(int startIndex)
第二種是:String substring(int startIndex,int endIndex)
1.14、concat() 連接兩個(gè)字符串
String s="abc";
String s1 = "abc";
System.out.println(s.concat(s1));//abcabc
1.15 、replace() 替換
它有兩種形式,第一種形式用一個(gè)字符在調(diào)用字符串中所有出現(xiàn)某個(gè)字符的地方進(jìn)行替換,形式如下:
String replace(char original,char replacement)
例如:String s="Hello".replace('l','w');//Hewwo
第二種形式是用一個(gè)字符序列替換另一個(gè)字符序列,形式如下:
String replace(CharSequence original,CharSequence replacement)
1.16、trim() 去掉起始和結(jié)尾的空格
1.17、valueOf() 轉(zhuǎn)換為字符串
1.18、toLowerCase() 轉(zhuǎn)換為小寫(xiě)
1.19、toUpperCase() 轉(zhuǎn)換為大寫(xiě)
1.20、downloadFile = new String(downloadFile.getBytes("ISO-8859-1"), "utf-8");字符集轉(zhuǎn)換
1.21、StringBuffer構(gòu)造函數(shù)
StringBuffer定義了三個(gè)構(gòu)造函數(shù):
StringBuffer()
StringBuffer(int size)
StringBuffer(String str)
StringBuffer(CharSequence chars)
(1)、length()和capacity()
一個(gè)StringBuffer當(dāng)前長(zhǎng)度可通過(guò)length()方法得到,而整個(gè)可分配空間通過(guò)capacity()方法得到。
(2)、ensureCapacity() 設(shè)置緩沖區(qū)的大小
void ensureCapacity(int capacity)
(3)、setLength() 設(shè)置緩沖區(qū)的長(zhǎng)度
void setLength(int len)
(4)、charAt()和setCharAt()
char charAt(int where)
void setCharAt(int where,char ch)
(5)、getChars()
void getChars(int sourceStart,int sourceEnd,char target[],int targetStart)
(6)、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();
(7)、insert() 插入字符串
StringBuffer insert(int index,String str)
StringBuffer insert(int index,char ch)
StringBuffer insert(int index,Object obj)
index指定將字符串插入到StringBuffer對(duì)象中的位置的下標(biāo)。
(8)、reverse() 顛倒StringBuffer對(duì)象中的字符
StringBuffer reverse()
(9)、delete()和deleteCharAt() 刪除字符
StringBuffer delete(int startIndex,int endIndex)
StringBuffer deleteCharAt(int loc)
(10)、replace() 替換
StringBuffer replace(int startIndex,int endIndex,String str)
(11)、substring() 截取子串
String substring(int startIndex)
String substring(int startIndex,int endIndex)
聯(lián)系客服