substring()
它有兩種形式,第一種是:String substring(int startIndex)
第二種是:String substring(int startIndex,int endIndex)

concat() 連接兩個字符串

replace() 替換
它有兩種形式,第一種形式用一個字符在調用字符串中所有出現某個字符的地方進行替換,形式如下:
String replace(char original,char replacement)
例如:String s=」Hello」.replace(』l',』w');
第二種形式是用一個字符序列替換另一個字符序列,形式如下:
String replace(CharSequence original,CharSequence replacement)

trim() 去掉起始和結尾的空格

valueOf() 轉換為字符串

toLowerCase() 轉換為小寫

toUpperCase() 轉換為大寫

length() 取得字符串的長度
例:
char chars[]={』a',』b'.』c'};
String s=new String(chars);
int len=s.length();

charAt() 截取一個字符
例:
char ch;
ch=」abc」.charAt(1); 
返回值為』b'

getChars() 截取多個字符
void getChars(int sourceStart,int sourceEnd,char target[],int targetStart)
sourceStart 指定了子串開始字符的下標
sourceEnd 指定了子串結束後的下一個字符的下標。因此,子串包含從sourceStart到sourceEnd-1的字符。
target 指定接收字符的數組
targetStart target中開始複製子串的下標值
例:
String s=」this is a demo of the getChars method.」;
char buf[]=new char[20];
s.getChars(10,14,buf,0);

getBytes()
替代getChars()的一種方法是將字符存儲在字節數組中,該方法即getBytes()
例:
String s = 「Hello!你好!」; 
byte[] bytes = s.getBytes();

toCharArray()
例:
String s = 「Hello!你好!」; 
char[] ss = s.toCharArray();

equals()和equalsIgnoreCase() 比較兩個字符串

regionMatches() 用於比較一個字符串中特定區域與另一特定區域,它有一個重載的形式允許在比較中忽略大小寫。
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()方法決定是否以特定字符串開始,endWith()方法決定是否以特定字符串結束

equals()和==
equals()方法比較字符串對象中的字符,==運算符比較兩個對象是否引用同一實例。
例:String s1=」Hello」;
String s2=new String(s1);
s1.eauals(s2); //true
s1==s2;//false

compareTo()和compareToIgnoreCase() 比較字符串

indexOf()和lastIndexOf()
indexOf() 查找字符或者子串第一次出現的地方。
lastIndexOf() 查找字符或者子串是後一次出現的地方。

StringBuffer構造函數
StringBuffer定義了三個構造函數:
StringBuffer()
StringBuffer(int size)
StringBuffer(String str)
StringBuffer(CharSequence chars)

下面是StringBuffer相關的函數:
length()和capacity()
一個StringBuffer當前長度可通過length()方法得到,而整個可分配空間通過capacity()方法得到。

ensureCapacity() 設置緩衝區的大小
void ensureCapacity(int capacity)

setLength() 設置緩衝區的長度
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() 可把任何類型數據的字符串表示連接到調用的StringBuffer對象的末尾。
例: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對象中的位置的下標。

reverse() 顛倒StringBuffer對象中的字符
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)

 

判斷字串編碼

import java.nio.charset.Charset;

public class Main {
public static void main(String[] args) {
for (String encoding : Charset.availableCharsets().keySet()) {
try {
// 當初是哪個位元組陣列被解釋為 UTF-8 的?一個一個嘗試!
byte[] binary = "®õ¤s½Y¥۱з|".getBytes(encoding);
System.out.printf("%s %s%n",
encoding, new String(binary, "Big5"));
} catch (Exception e) {
}
}
}
}

 

 

轉貼出處:http://www.dotblogs.com.tw/yc421206/archive/2008/11/03/5862.aspx

 

arrow
arrow
    全站熱搜

    Will(小威) 發表在 痞客邦 留言(0) 人氣()