Java - 字串類



描述

字串在 Java 程式設計中被廣泛使用,它是一系列字元。在 Java 程式語言中,字串被視為物件。

Java 平臺提供 String 類來建立和操作字串。

建立字串

建立字串最直接的方法是編寫:

String greeting = "Hello world!";

每當它在你的程式碼中遇到字串字面量時,編譯器都會建立一個 String 物件,其值為在這種情況下,“Hello world!”。

與任何其他物件一樣,你可以使用 new 關鍵字和建構函式來建立 String 物件。String 類有 11 個建構函式,允許你使用不同的來源(例如字元陣列)來提供字串的初始值。

從字元陣列建立字串示例

public class StringDemo {

   public static void main(String args[]) {
      char[] helloArray = { 'h', 'e', 'l', 'l', 'o', '.' };
      String helloString = new String(helloArray);  
      System.out.println( helloString );
   }
}

這將產生以下結果:

輸出

hello.

注意 - String 類是不可變的,因此一旦建立了 String 物件,就無法更改它。如果需要對字串字元進行大量修改,則應使用 StringBuffer & StringBuilder 類。

字串長度

用於獲取有關物件資訊的方法稱為訪問器方法。你可以與字串一起使用的一個訪問器方法是 length() 方法,它返回字串物件中包含的字元數。

以下程式是length()方法 String 類的示例。

獲取字串長度示例

public class StringDemo {

   public static void main(String args[]) {
      String palindrome = "Dot saw I was Tod";
      int len = palindrome.length();
      System.out.println( "String Length is : " + len );
   }
}

這將產生以下結果:

輸出

String Length is : 17

連線字串

String 類包含一個用於連線兩個字串的方法:

string1.concat(string2);

它返回一個新的字串,該字串是 string1,string2 附加到其末尾。你也可以將 concat() 方法與字串字面量一起使用,如下所示:

"My name is ".concat("Zara");

字串通常使用 + 運算子連線,如下所示:

"Hello," + " world" + "!"

結果為:

"Hello, world!"

讓我們看看下面的例子:

連線字串示例

public class StringDemo {

   public static void main(String args[]) {
      String string1 = "saw I was ";
      System.out.println("Dot " + string1 + "Tod");
   }
}

這將產生以下結果:

輸出

Dot saw I was Tod

建立格式化字串

你擁有 printf() 和 format() 方法來列印具有格式化數字的輸出。String 類有一個等效的類方法 format(),它返回一個 String 物件而不是 PrintStream 物件。

使用 String 的靜態 format() 方法,你可以建立一個可以重複使用的格式化字串,而不是一次性列印語句。例如,而不是:

格式化字串示例

System.out.printf("The value of the float variable is " +
   "%f, while the value of the integer " +
   "variable is %d, and the string " +
   "is %s", floatVar, intVar, stringVar);

你可以編寫:

String fs;
fs = String.format("The value of the float variable is " +
   "%f, while the value of the integer " +
   "variable is %d, and the string " +
   "is %s", floatVar, intVar, stringVar);
System.out.println(fs);

字串方法

以下是 String 類支援的方法列表:

序號 方法和描述
1 char charAt(int index)

此方法返回指定索引處的 char 值。

2 int codePointAt(int index)

此方法返回指定索引處的字元(Unicode 程式碼點)。

3 int codePointBefore(int index)

此方法返回指定索引之前的字元(Unicode 程式碼點)。

4 int codePointCount(int beginIndex, int endIndex)

此方法返回此 String 指定文字範圍內的 Unicode 程式碼點數。

5 int compareTo(String anotherString)

此方法按字典順序比較兩個字串。

6 int compareToIgnoreCase(String str)

此方法按字典順序比較兩個字串,忽略大小寫差異。

7 String concat(String str)

此方法將指定的字串連線到此字串的末尾。

8 boolean contains(CharSequence s)

當且僅當此字串包含指定的 char 值序列時,此方法返回 true。

9 boolean contentEquals(CharSequence cs)

此方法將此字串與指定的 CharSequence 進行比較。

10 static String copyValueOf(char[] data)

此方法返回一個表示指定陣列中字元序列的 String。

11 boolean endsWith(String suffix)

此方法測試此字串是否以指定的 suffix 結尾。

12 boolean equals(Object anObject)

此方法將此字串與指定的物件進行比較。

13 boolean equalsIgnoreCase(String anotherString)

此方法將此 String 與另一個 String 進行比較,忽略大小寫考慮因素。

14 static String format(String format, Object... args)

此方法使用指定的格式字串和引數返回格式化的字串。

15 byte[] getBytes()

此方法使用平臺的預設字元集將此 String 編碼為一系列位元組,並將結果儲存到新的位元組陣列中。

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

此方法將字元從此字串複製到目標字元陣列。

17 int hashCode()

此方法返回此字串的雜湊碼。

18 int indexOf(int ch)

此方法返回此字串中指定字元第一次出現的索引。

19 String intern()

此方法返回字串物件的規範表示形式。

20 boolean isEmpty()

當且僅當 length() 為 0 時,此方法返回 true。

21 int lastIndexOf(int ch)

此方法返回此字串中指定字元最後一次出現的索引。

22 int length()

此方法返回此字串的長度。

23 boolean matches(String regex)

此方法指示此字串是否與給定的正則表示式匹配。

24 int offsetByCodePoints(int index, int codePointOffset)

此方法返回此 String 中相對於給定索引偏移 codePointOffset 程式碼點的索引。

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

此方法測試兩個字串區域是否相等。

26 String replace(char oldChar, char newChar)

此方法返回一個新的字串,該字串是透過用 newChar 替換此字串中所有出現的 oldChar 而生成的。

27 String replaceAll(String regex, String replacement)

此方法用給定的 replacement 替換此字串中與給定正則表示式匹配的每個子字串。

28 String replaceFirst(String regex, String replacement)

此方法用給定的 replacement 替換此字串中第一個與給定正則表示式匹配的子字串。

29 String[] split(String regex)

此方法根據給定正則表示式的匹配項拆分此字串。

30 boolean startsWith(String prefix)

此方法測試此字串是否以指定的 prefix 開頭。

31 CharSequence subSequence(int beginIndex, int endIndex)

此方法返回一個新的字元序列,它是此序列的子序列。

32 String substring(int beginIndex)

此方法返回一個新的字串,它是此字串的子字串。

33 char[] toCharArray()

此方法將此字串轉換為新的字元陣列。

34 String toLowerCase()

此方法使用預設區域設定的規則將此 String 中的所有字元轉換為小寫。

35 String toString()

此方法返回字串本身。

36 String toUpperCase()

此方法使用預設區域設定的規則將此 String 中的所有字元轉換為大寫。

37 String trim()

此方法返回字串的副本,省略了前導和尾隨空格。

38 static String valueOf(boolean b)

此方法返回布林引數的字串表示形式。

廣告