
- Java 13 Tutorial
- Java 13 - Home
- Java 13 - Overview
- Java 13 - Environment Setup
- Java 13 Language Changes
- Java 13 - Switch Expressions
- Java 13 - Text Blocks
- Java 13 - Text Block Methods
- Java 13 - Socket API Reimplementation
- Java 13 - Miscellaneous Changes
- Java 13 JVM Changes
- Java 13 - Dynamic CDS Archive
- Java 13 - ZGC Enhancements
- Java Other Versions Tutorials
- Java Tutorial
- Java 8 Tutorial
- Java 9 Tutorial
- Java 10 Tutorial
- Java 11 Tutorial
- Java 12 Tutorial
- Java 13 Useful Resources
- Java 13 - Quick Guide
- Java 13 - Useful Resources
- Java 13 - Discussion
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java 13 - Text Block Methods
Java 12 introduces text blocks to handle multiline strings like JSON/XML/HTML etc and added new methods to String class to handle text blocks. It is a preview feature.
stripIndent() - removes incidental white spaces from the start and end of the string.
translateEscapes() - translate the escape sequences as per the string syntax.
formatted() - similar to String format() method to support formatting in text block strings.
Example
Consider the following example −
ApiTester.java
public class APITester { public static void main(String[] args) { String textBlockJSON = """ { "name" : "%s", "RollNO" : "%s" } """.formatted("Mahesh", "32"); System.out.println(textBlockJSON); } }
Compile and Run the program
$javac -Xlint:preview --enable-preview -source 13 APITester.java $java --enable-preview APITester
Output
{ "Name" : "Mahesh", "RollNO" : "32" } { "name" : "Mahesh", "RollNO" : "32" } Contains: true indexOf: 15 Length: 45
Advertisements