🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
String.stripIndent() method in Java is used to remove incidental leading whitespace from all lines in a string and then remove leading and trailing blank lines. This guide will cover the method's usage, explain how it works, and provide examples to demonstrate its functionality.Table of Contents
- Introduction
stripIndentMethod Syntax- Examples
- Removing Leading Whitespace
- Handling Blank Lines
- Conclusion
Introduction
The String.stripIndent() method is a member of the String class in Java, introduced in Java 13. It allows you to remove incidental leading whitespace from all lines in a string and remove leading and trailing blank lines. This is particularly useful for formatting multiline strings, especially when working with text blocks.
stripIndent Method Syntax
The syntax for the stripIndent method is as follows:
public String stripIndent()
Examples
Removing Leading Whitespace
The stripIndent method can be used to remove incidental leading whitespace from each line in a multiline string.
Example
public class StripIndentExample {
public static void main(String[] args) {
String textBlock = """
Line 1
Line 2
Line 3
""";
String strippedText = textBlock.stripIndent();
System.out.println("Original text block:");
System.out.println(textBlock);
System.out.println("Stripped text block:");
System.out.println(strippedText);
}
}
Output:
Original text block:
Line 1
Line 2
Line 3
Stripped text block:
Line 1
Line 2
Line 3
Handling Blank Lines
The stripIndent method also removes leading and trailing blank lines in addition to removing incidental leading whitespace from all lines.
Example
public class StripIndentExample {
public static void main(String[] args) {
String textBlock = """
Line 1
Line 2
Line 3
""";
String strippedText = textBlock.stripIndent();
System.out.println("Original text block:");
System.out.println(textBlock);
System.out.println("Stripped text block:");
System.out.println(strippedText);
}
}
Output:
Original text block:
Line 1
Line 2
Line 3
Stripped text block:
Line 1
Line 2
Line 3
Using with Text Blocks
The stripIndent method is especially useful when working with text blocks to ensure consistent indentation.
Example
public class StripIndentExample {
public static void main(String[] args) {
String textBlock = """
{
"name": "Alice",
"age": 30,
"city": "Wonderland"
}
""";
String strippedText = textBlock.stripIndent();
System.out.println("Original text block:");
System.out.println(textBlock);
System.out.println("Stripped text block:");
System.out.println(strippedText);
}
}
Output:
Original text block:
{
"name": "Alice",
"age": 30,
"city": "Wonderland"
}
Stripped text block:
{
"name": "Alice",
"age": 30,
"city": "Wonderland"
}
Conclusion
The String.stripIndent() method in Java provides a convenient way to remove incidental leading whitespace from all lines in a string and remove leading and trailing blank lines. By understanding how to use this method, you can efficiently format multiline strings in your Java applications. Whether you are working with text blocks or handling multiline strings with leading whitespace, the stripIndent method offers a reliable solution for these tasks.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment