English 中文(简体)
Java 15 - Text Blocks
  • 时间:2024-09-17

Java 15 - Text Blocks


Previous Page Next Page  

Java 13 introduces text blocks to handle multipne strings pke JSON/XML/HTML etc as is a preview feature. With Java 14, we ve second preview of Text Blocks. Now Text Block is no more a preview feature and is a part of standard offering.

Example

Consider the following example −

ApiTester.java


pubpc class APITester {
   pubpc static void main(String[] args) {
      String stringJSON = "{
" 
         + ""Name" : "Mahesh"," 
         + ""RollNO" : "32"
" 
         + "}";  
      System.out.println(stringJSON);
	   String textBlockJSON = """{"name" : "Mahesh", "RollNO" : "32"}""";
      System.out.println(textBlockJSON);
	   System.out.println("Contains: " + textBlockJSON.contains("Mahesh"));
	   System.out.println("indexOf: " + textBlockJSON.indexOf("Mahesh"));
	   System.out.println("Length: " + textBlockJSON.length());
   }
}

Compile and Run the program


$javac APITester.java
$java APITester

Output


{
   "Name" : "Mahesh","RollNO" : "32"
}
{
   "name" : "Mahesh",   "RollNO" : "32"
}
Contains: true
indexOf: 15
Length: 45
Advertisements