(3)利用字節數組生成字符串:
byte c1[]={66,67,68};
byte c2[]={65,66,67,68};
String str1=new String(c1);
String str2=new String(c2,1,3);
System.out.print(str1);
System.out.print(str1);
結果均是BCD
String類的常用方法
String類提供了length(),charAt(),indexOf(),lastIndexOf(),getChars(),getBytes(),toCharArray()等方法。這些方法中按照用途分為字符串長度計算、字符串比較、字符串檢索、字符串的截取、替換等方法。
字符串長度計算
使用String類中的方法length()方法可以獲取一個字符串的長度。定義如下:
public int length()
該方法返回字符串的16-bit的Unicode字符的數量。例如:
String s="xjtu";
s.length()為4
字符串比較
字符串比較的方法有equals()、equalsIgnoreCase()、startsWith()、endsWith()、regionMatches()、compareTo()、compareToIgnoreCase()等方法。
1)equals()和equalsIgnoreCase()方法
equals()定義:
public boolean equals(String s)
該方法用于比較當前字符串對象的實體是否與參數指定的字符串s的實體相同。注意String類中的equals()方法和"=="的區別。
equalsIgnoreCase()定義:
public booleanequalsIgnoreCase(String s)
字符串對象調用調用equalsIgnoreCase(String s)來比較當前對象是否與參數指定的字符串s相同,比較時忽略大小寫。
2)startsWith()和endsWith()方法
字符串對象調用startsWith()和endsWith()方法分別判斷當前字符串對象的前綴和后綴是否是參數指定的字符串s。
定義:
public booleanstartsWith(String s)和public boolean endsWith(String s);
例如:
String str1="20150418";
String str2="20140418";
str1.startsWith("2015")的值是true;
str2.endsWith("0418")的值是true;
3)regionMatches()方法
聲明格式如下:
public booleanregionMatches(int firstStart,String other,int otherStart,int length)和public booleanregionMatches(boolean b,int firstStart,String other,int otherStart,int length)
從當前指定的字符串參數firstStart指定的位置開始處,取長度為length的一個子串,并將這個子串與參數other指定的一個子串進行比較。其中other指定的子串是是從參數otherStart指定的位置開始,從other中取長度為length的一個子串。如果兩個子串相同就返回true,否則false。
4)compareTo()和compareToIgnoreCase()方法
聲明格式:
public intcompareTo(String s)
public intcompareToIgnoreCase(String s)
compareTo()方法按照字典順序與參數s指定的字符串比較大小。如果當前字符串與s相同則返回0;如果如果大于s則返回正值,小于s返回負值。注意小寫字母大于大寫字母,而compareToIgnoreCase()方法忽略大小寫。
字符串檢索
搜索指定字符或字符串在字符串中出現的位置,用于字符或字符串在字符串中的定位。方法聲明格式如下:
public int indexOf(int ch)
public int indexOf(int ch,int fromIndex)
public int indexOf(String str)
public int indexOf(String str,int fromIndex)
上述四個重載方法分別用于在字符串中定位指定的字符和字符串,并且在方法種可以通過fromIndex來指定匹配的起始地址。如果沒有檢索到字符或字符串,該方法返回值是-1。
String s="xjtuxjtu";
int i;
i=s.indexOf('x');//0
i=s.indexOf("xjtu",0);//0
i=s.indexOf('xjtu',4);//4
另外,String類還提供字符串中的最后位置的定位,方法聲明格式如下:
public int lastIndexOf(int ch)
public int lastIndexOf(int ch,int fromIndex)
public int lastIndexOf(String str)
public int lastIndexOf(String str,int fromIndex)
上述4個重載方法分別用于在字符串中定位定位指定的字符和字符串最后出現的位置,并且在上述方法中可以通過fromIndex來指定匹配的起始位置。如果沒有檢索到字符或字符串,該方法返回值是-1。
字符串截取
在字符串中截取子字符串,其聲明格式:
public String substring(int beginIndex)
該方法將獲得一個當前字符串的子串,該子串是從當前字符串的beginIndex處截取到最后所得到的字符串。
public String substring(int beginIndex,int endIndex)
該方法將獲得一個當前字符串的子串,該子串是從當前字符串的beginIndex處截取到endIndex-1結束處所得到的字符串。
字符串的替換
public String replace(char oldChar,char newChar)
字符串對象s調用該方法可以獲得一個串對象,這個串對象是用參數newChar指定的字符替換s中的oldChar指定對所有的字符而得到的字符串。
public String replace(String old,String new)
字符串對象s調用該方法可以獲得一個串對象,這個串對象是用參數new指定的字符串替換s中的old指定對所有的字符串而得到的字符串。
String s="xjtuxjtu";System.out.println(s.replace('x','s'));//sjtusjtu
System.out.println(s.replace("xjtu","Tsinghua"));//TsinghuaTsinghua
public String trim()
一個字符串s通過調用方法trim()得到一個字符串對象,該字符串對象是s去掉前后空格后的字符串。
其他的一些方法
1)字符串大小寫轉換
聲明格式如下:
public String toUpperCase(Local local)//僅對指定位置轉換
public String toUpperCase()
public String toLowerCase(Local local)// 僅對指定位置轉換
public String toLowerCase()
2)轉換為字符串數組
聲明格式如下:
public char[] toCharArray()
該方法用于將字符串轉換為字符串數組。該方法的返回值類型為字符數組,如下
String str=new String("I love xjtu");
char[] ch;
ch=str.toCharArray();
System.out.print(ch);//I love xjtu
3)字符串到字符數組之間的轉換
聲明格式:
getChars(int srcBegin,int srcEnd,char[] dst,int dstBegin)
該方法用于將字符串中的字符內容復制到字符數組中。其中srcBegin為復制的起始位置,srcEnd-1為復制的終止位置、字符串數值dst為目的字符數組,dstBegin為目的字符數組的復制的起始位置。復制的時候會覆蓋原來位置數據。
4)連接兩個字符串
聲明格式如下:
public String concat(String str)
該方法用于將兩個字符串連接在一起,與字符串的"+" 操作符功能相同。