天天看點

Java String 類

字元串廣泛應用 在 Java 程式設計中,在 Java 中字元串屬于對象,Java 提供了 String 類來建立和操作字元串。

建立字元串最簡單的方式如下:

String str = "Runoob";

在代碼中遇到字元串常量時,這裡的值是 "Runoob",編譯器會使用該值建立一個 String 對象。

和其它對象一樣,可以使用關鍵字和構造方法來建立 String 對象。

用構造函數建立字元串:

String str2=new String("Runoob");

String 建立的字元串存儲在公共池中,而 new 建立的字元串對象在堆上:

String s1 = "Runoob"; // String 直接建立

String s2 = "Runoob"; // String 直接建立

String s3 = s1; // 相同引用

String s4 = new String("Runoob"); // String 對象建立

String s5 = new String("Runoob"); // String 對象建立

Java String 類

String 類有 11 種構造方法,這些方法提供不同的參數來初始化字元串,比如提供一個字元數組參數:

public class StringDemo{

public static void main(String args[]){

char[] helloArray = { 'r', 'u', 'n', 'o', 'o', 'b'};

String helloString = new String(helloArray);

System.out.println( helloString );

}

以上執行個體編譯運作結果如下:

注意:String 類是不可改變的,是以你一旦建立了 String 對象,那它的值就無法改變了(詳看筆記部分解析)。

如果需要對字元串做很多修改,那麼應該選擇使用 StringBuffer & StringBuilder 類。

用于擷取有關對象的資訊的方法稱為通路器方法。

String 類的一個通路器方法是 length() 方法,它傳回字元串對象包含的字元數。

下面的代碼執行後,len 變量等于 14:

public class StringDemo {

public static void main(String args[]) {

String site = "www.runoob.com";

int len = site.length();

System.out.println( "菜鳥教程網址長度 : " + len );

String 類提供了連接配接兩個字元串的方法:

string1.concat(string2);

傳回 string2 連接配接 string1 的新字元串。也可以對字元串常量使用 concat() 方法,如:

更常用的是使用'+'操作符來連接配接字元串,如:

結果如下:

下面是一個例子:

String string1 = "菜鳥教程網址:";

System.out.println("1、" + string1 + "www.runoob.com");

我們知道輸出格式化數字可以使用 printf() 和 format() 方法。

String 類使用靜态方法 format() 傳回一個String 對象而不是 PrintStream 對象。

String 類的靜态方法 format() 能用來建立可複用的格式化字元串,而不僅僅是用于一次列印輸出。

如下所示:

System.out.printf("浮點型變量的值為 " +

"%f, 整型變量的值為 " +

" %d, 字元串變量的值為 " +

"is %s", floatVar, intVar, stringVar);

你也可以這樣寫

String fs;

fs = String.format("浮點型變量的值為 " +

" %s", floatVar, intVar, stringVar);

下面是 String 類支援的方法,更多詳細,參看 Java String API 文檔:

SN(序号)

方法描述

1

char charAt(int index)

傳回指定索引處的 char 值。

2

int compareTo(Object o)

把這個字元串和另一個對象比較。

3

int compareTo(String anotherString)

按字典順序比較兩個字元串。

4

int compareToIgnoreCase(String str)

按字典順序比較兩個字元串,不考慮大小寫。

5

String concat(String str)

将指定字元串連接配接到此字元串的結尾。

6

boolean contentEquals(StringBuffer sb)

當且僅當字元串與指定的StringBuffer有相同順序的字元時候傳回真。

7

static String copyValueOf(char[] data)

傳回指定數組中表示該字元序列的 String。

8

static String copyValueOf(char[] data, int offset, int count)

9

boolean endsWith(String suffix)

測試此字元串是否以指定的字尾結束。

10

boolean equals(Object anObject)

将此字元串與指定的對象比較。

11

boolean equalsIgnoreCase(String anotherString)

将此 String 與另一個 String 比較,不考慮大小寫。

12

byte[] getBytes()

 使用平台的預設字元集将此 String 編碼為 byte 序列,并将結果存儲到一個新的 byte 數組中。

13

byte[] getBytes(String charsetName)

使用指定的字元集将此 String 編碼為 byte 序列,并将結果存儲到一個新的 byte 數組中。

14

void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)

将字元從此字元串複制到目标字元數組。

15

int hashCode()

傳回此字元串的哈希碼。

16

int indexOf(int ch)

傳回指定字元在此字元串中第一次出現處的索引。

17

int indexOf(int ch, int fromIndex)

傳回在此字元串中第一次出現指定字元處的索引,從指定的索引開始搜尋。

18

int indexOf(String str)

 傳回指定子字元串在此字元串中第一次出現處的索引。

19

int indexOf(String str, int fromIndex)

傳回指定子字元串在此字元串中第一次出現處的索引,從指定的索引開始。

20

String intern()

 傳回字元串對象的規範化表示形式。

21

int lastIndexOf(int ch)

 傳回指定字元在此字元串中最後一次出現處的索引。

22

int lastIndexOf(int ch, int fromIndex)

傳回指定字元在此字元串中最後一次出現處的索引,從指定的索引處開始進行反向搜尋。

23

int lastIndexOf(String str)

傳回指定子字元串在此字元串中最右邊出現處的索引。

24

int lastIndexOf(String str, int fromIndex)

 傳回指定子字元串在此字元串中最後一次出現處的索引,從指定的索引開始反向搜尋。

25

int length()

傳回此字元串的長度。

26

boolean matches(String regex)

告知此字元串是否比對給定的正規表達式。

27

boolean regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len)

測試兩個字元串區域是否相等。

28

boolean regionMatches(int toffset, String other, int ooffset, int len)

29

String replace(char oldChar, char newChar)

傳回一個新的字元串,它是通過用 newChar 替換此字元串中出現的所有 oldChar 得到的。

30

String replaceAll(String regex, String replacement)

使用給定的 replacement 替換此字元串所有比對給定的正規表達式的子字元串。

31

String replaceFirst(String regex, String replacement)

 使用給定的 replacement 替換此字元串比對給定的正規表達式的第一個子字元串。

32

String[] split(String regex)

根據給定正規表達式的比對拆分此字元串。

33

String[] split(String regex, int limit)

根據比對給定的正規表達式來拆分此字元串。

34

boolean startsWith(String prefix)

測試此字元串是否以指定的字首開始。

35

boolean startsWith(String prefix, int toffset)

測試此字元串從指定索引開始的子字元串是否以指定字首開始。

36

CharSequence subSequence(int beginIndex, int endIndex)

 傳回一個新的字元序列,它是此序列的一個子序列。

37

String substring(int beginIndex)

傳回一個新的字元串,它是此字元串的一個子字元串。

38

String substring(int beginIndex, int endIndex)

傳回一個新字元串,它是此字元串的一個子字元串。

39

char[] toCharArray()

将此字元串轉換為一個新的字元數組。

40

String toLowerCase()

使用預設語言環境的規則将此 String 中的所有字元都轉換為小寫。

41

String toLowerCase(Locale locale)

 使用給定 Locale 的規則将此 String 中的所有字元都轉換為小寫。

42

String toString()

 傳回此對象本身(它已經是一個字元串!)。

43

String toUpperCase()

使用預設語言環境的規則将此 String 中的所有字元都轉換為大寫。

44

String toUpperCase(Locale locale)

使用給定 Locale 的規則将此 String 中的所有字元都轉換為大寫。

45

String trim()

傳回字元串的副本,忽略前導空白和尾部空白。

46

static String valueOf(primitive data type x)

傳回給定data type類型x參數的字元串表示形式。

47

contains(CharSequence chars)

判斷是否包含指定的字元系列。

48

isEmpty()

判斷字元串是否為空。