🎓 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
In this guide, you will learn about the String stripTrailing() method in Java programming and how to use it with an example.
1. String stripTrailing() Method Overview
Definition:
The stripTrailing() method is a member of the Java String class, introduced in Java 11. It is utilized to eliminate all trailing whitespace characters from a given string, based on the Unicode Character Property “White_Space”. The method is more comprehensive than trim(), which only removes space characters, as it deals with any Unicode whitespace character.
Syntax:
public String stripTrailing()
Parameters:
The stripTrailing() method does not take any parameters.
Key Points:
- It removes all types of trailing whitespaces as per the Unicode standard.
- The method returns the original string if there are no trailing whitespaces.
- It doesn’t affect leading whitespaces.
2. String stripTrailing() Method Example
public class StripTrailingExample {
public static void main(String[] args) {
String str = " \t \u2005Java\u2005 \n";
String strippedString = str.stripTrailing();
System.out.println("Original String: [" + str + "]");
System.out.println("String with Trailing Whitespaces Stripped: [" + strippedString + "]");
}
}
Output:
Original String: [ Java ] String with Trailing Whitespaces Stripped: [ Java]
Explanation:
In this example, a string str is defined with different types of whitespaces (tabs, spaces, and the Unicode character \u2005 - Four-Per-Em Space) surrounding the word "Java".
The stripTrailing() method is invoked to remove all trailing whitespaces while leaving the leading whitespaces intact. The resultant modified string is then outputted to the console, illustrating the effectiveness of the stripTrailing() method.
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