Java 教程

Java 控制語句

面向物件程式設計

Java 內建類

Java 檔案處理

Java 錯誤與異常

Java 多執行緒

Java 同步

Java 網路程式設計

Java 集合

Java 介面

Java 資料結構

Java 集合演算法

高階 Java

Java 雜項

Java APIs 與框架

Java 類引用

Java 有用資源

Java - 文字塊



Java 在 Java 15 中引入了 **文字塊** 作為標準特性,用於處理多行 字串,例如 JSON/XML/HTML 等。它在 Java 13 中作為預覽特性引入。

引入文字塊的主要目的是更有效地宣告多行字串。在文字塊之前,我們可以使用字串連線、字串生成器追加方法、字串連線方法來宣告多行字串,但這種方法相當混亂。因為我們必須使用行終止符、分隔符等來標記新行。文字塊提供了一種更好、更替代的方法來使用 """,三個雙引號標記來定義多行字串。

文字塊語法

**文字塊**是對現有 String 物件的增強,具有特殊的語法,其中字串內容應以 """ 開頭,並以換行符結尾,並以 """ 結尾。""" 內的任何內容都將按原樣使用。

String textBlockJSON = """
   {
      "name" : "Mahesh",
      "RollNO" : "32"
   }
   """;

等效字串可以使用如下所示的舊語法編寫

String stringJSON = "{\r\n" 
         + "   \"Name\" : \"Mahesh\",\r\n" 
         + "   \"RollNO\" : \"32\"\r\n" 
         + "}";  

Java 文字塊示例

在這個例子中,我們使用文字塊和字串連線列印了 json 字串。

package com.tutorialspoint;

public class Tester {

   public static void main(String[] args) {
      String stringJSON = "{\r\n" 
      + "   \"Name\" : \"Mahesh\",\r\n" 
      + "   \"RollNO\" : \"32\"\r\n" 
      + "}";  

      System.out.println(stringJSON);

      String textBlockJSON = """
      {
         "name" : "Mahesh",
         "RollNO" : "32"
      }
      """;
      System.out.println(textBlockJSON);
   }   
}

輸出

讓我們編譯並執行上面的程式,這將產生以下結果:

{
   "Name" : "Mahesh",
   "RollNO" : "32"
}
{
   "name" : "Mahesh",
   "RollNO" : "32"
}

文字塊字串操作

文字塊與 String 相同,可以使用 equals() 方法 或相等運算子進行比較。

// compare the content, 
textBlockJSON.equals(stringJSON);
	      
// compare the objects
textBlockJSON == stringJSON;

文字塊支援所有字串操作,如 indexOf()、contains() 等。

// check if text block contains a provided string or not
textBlockJSON.contains("Mahesh");

// get the length of string content
textBlockJSON.length()

示例:Java 中的文字塊字串操作

在這個例子中,我們執行了各種字串操作,並將文字塊與等效字串進行了比較。

package com.tutorialspoint;

public class Tester {

   public static void main(String[] args) {
      String stringJSON = "Mahesh";

      String textBlockJSON = """
      Mahesh""";
      // compare the content
      System.out.println(textBlockJSON.equals(stringJSON));

      // compare the objects
      System.out.println(textBlockJSON == stringJSON);

      // text block supports all string operations
      System.out.println("Contains: " + textBlockJSON.contains("Mahesh"));
      System.out.println("indexOf: " + textBlockJSON.indexOf("Mahesh"));
      System.out.println("Length: " + textBlockJSON.length());
   }   
}

輸出

讓我們編譯並執行上面的程式,這將產生以下結果:

true
true
Contains: true
indexOf: 0
Length: 6

文字塊方法

  • **stripIndent()** - 從字串的開頭和結尾刪除不必要的空格。

  • **translateEscapes()** - 根據字串語法轉換轉義序列。

  • **formatted()** - 類似於 String format() 方法,支援在文字塊字串中進行格式化。

示例

考慮以下示例:

ApiTester.java

public class APITester {

   public static void main(String[] args) {
	  String textBlockJSON = """
         {
            "name" : "%s",
            "RollNO" : "%s"
         }
         """.formatted("Mahesh", "32");
      System.out.println(textBlockJSON);
   }   
}

輸出

{
   "name" : "Mahesh",
   "RollNO" : "32"
}
廣告