DIV CSS 佈局教程網

Java 常用 String 與 StringBuffer 方法彙整
編輯:JavaScript綜合知識     

在 Java 裡,字串同樣是由一連串字元組成。但與許多把字串當作字元陣列處理的語言不同,Java 將字串視為 String 型別的物件,這讓它能提供豐富的功能來處理字串。以下整理幾個使用頻率相當高的方法與說明。

String 相關方法

1) substring()
它有兩種形式:
String substring(int startIndex)
String substring(int startIndex, int endIndex)

2) concat() 連接兩個字串
例:String s = "Welcome to ";
     String t = s.concat("AnHui");

3) replace() 替換
第一種形式以單一字元取代所有出現之處:
String replace(char original, char replacement)
例:String s = "Hello".replace('l', 'w');
第二種形式以字元序列取代另一字元序列:
String replace(CharSequence original, CharSequence replacement)

4) trim() 去除頭尾的空白
5) valueOf() 轉換為字串
6) toLowerCase() 轉為小寫
7) toUpperCase() 轉為大寫

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

9) charAt() 擷取單一字元
例:char ch = "abc".charAt(1); // 傳回 'b'

10) 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);

11) getBytes() 將字元存入位元組陣列,可作為 getChars() 的替代方案
例:String s = "Hello!你好!";
byte[] bytes = s.getBytes();

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

13) equals() 與 equalsIgnoreCase() 比較兩個字串
14) regionMatches() 比較某字串特定區域與另一字串的特定區域,並有可忽略大小寫的重載形式:
boolean regionMatches(int startIndex, String str2, int str2StartIndex, int numChars)
boolean regionMatches(boolean ignoreCase, int startIndex, String str2, int str2StartIndex, int numChars)

15) startsWith() 與 endsWith()
startsWith() 判斷是否以特定字串開頭;endsWith() 判斷是否以特定字串結尾。

16) equals() 與 ==
equals() 比較字串物件中的字元內容,== 運算子則比較兩個物件是否參照同一個實例。
例:String s1 = "Hello";
String s2 = new String(s1);
s1.equals(s2); // true
s1 == s2; // false

17) compareTo() 與 compareToIgnoreCase() 比較字串
18) indexOf() 與 lastIndexOf()
indexOf() 找出字元或子字串第一次出現的位置;lastIndexOf() 找出最後一次出現的位置。

19) trim 去空白
例:String t1 = "     abc    de     ";
System.out.println(t1.trim()); // 去掉頭尾空白,輸出 "abc   de"

20) split 字串分割
String y = "abc,de,fg,hi,jk";
String[] y1 = y.split(","); // 以所有 "," 為界切割
for (int i = 0; i < y1.length; i++) {
   System.out.print(y1[i]); // 輸出 abcdefghijk
}

21) append 附加或插入
StringBuffer zz1 = new StringBuffer(z1); // 用 append 插入字元
zz1.append('|').append("hijk").append('/').append("lmn").append("opq");
System.out.print(zz1); // 輸出:abcdefg|hijk/lmnopq

StringBuffer 相關方法

StringBuffer 定義了三個建構子:
StringBuffer()
StringBuffer(int size)
StringBuffer(String str)
StringBuffer(CharSequence chars)

1) length() 與 capacity() 前者取得目前長度,後者取得可指派的總空間。
2) ensureCapacity() 設定緩衝區大小:void ensureCapacity(int capacity)
3) setLength() 設定緩衝區長度: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() 可把任何型別資料的字串表示附加到 StringBuffer 末尾。
例: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 指定插入位置的下標。

8) reverse() 反轉 StringBuffer 中的字元: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)

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved