🎓 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 strip() method in Java programming and how to use it with an example.
1. String strip() Method Overview
Definition:
The strip() method in Java is part of the String class and was introduced in Java 11. It is used to remove the leading and trailing white spaces from a string. Unlike trim(), which considers anything less than or equal to U+0020 (space character) as whitespace, strip() uses the Unicode Character Property “White_Space” to identify whitespaces.
Syntax:
public String strip()
Parameters:
The method does not take any parameters.
Key Points:
- It removes all kinds of leading and trailing whitespaces as defined by the Unicode standard, not just the space character.
- If the string has no leading or trailing whitespaces, the same string is returned.
- It is Unicode-aware, making it more versatile than trim() for strings that may contain non-ASCII characters.
2. String strip() Method Example
public class StripExample {
public static void main(String[] args) {
String str = " \t \u2005Java\u2005 \n";
String strippedString = str.strip();
System.out.println("Original String: [" + str + "]");
System.out.println("Stripped String: [" + strippedString + "]");
}
}
Output:
Original String: [ Java ] Stripped String: [Java]
Explanation:
In this example, the str string contains various types of whitespaces including tabs, spaces, and the Unicode character \u2005 (Four-Per-Em Space) around the word "Java".
After calling the strip() method, all the leading and trailing whitespaces are removed, and only the word "Java" is left, which is printed to the console.
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