🎓 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
The Character.isWhitespace() method in Java is used to determine if a specified character is a whitespace character.
Table of Contents
- Introduction
isWhitespace()Method Syntax- Examples
- Checking Single Characters
- Handling Non-Whitespace Characters
- Checking Multiple Characters
- Real-World Use Case
- Conclusion
Introduction
The Character.isWhitespace() method is a static method in the Character class in Java. It checks whether a given character is a whitespace character. Whitespace characters include spaces, tabs, and other characters that separate words and symbols in text.
isWhitespace()() Method Syntax
The Character.isWhitespace() method has two overloaded versions:
isWhitespace(char ch)
public static boolean isWhitespace(char ch)
- ch: The character to be tested.
The method returns:
trueif the character is a whitespace character.falseotherwise.
isWhitespace(int codePoint)
public static boolean isWhitespace(int codePoint)
- codePoint: The Unicode code point to be tested.
The method returns:
trueif the code point is a whitespace character.falseotherwise.
Examples
Checking Single Characters
The isWhitespace(char ch) method can be used to check if a single character is a whitespace character.
Example
public class IsWhitespaceExample {
public static void main(String[] args) {
char spaceChar = ' ';
char nonSpaceChar = 'A';
boolean isWhitespace1 = Character.isWhitespace(spaceChar);
boolean isWhitespace2 = Character.isWhitespace(nonSpaceChar);
System.out.println("Is ' ' a whitespace character? " + isWhitespace1);
System.out.println("Is 'A' a whitespace character? " + isWhitespace2);
}
}
Output:
Is ' ' a whitespace character? true
Is 'A' a whitespace character? false
In this example, the method returns true for the space character ' ' and false for the non-whitespace character 'A'.
Checking Unicode Code Points
The isWhitespace(int codePoint) method can be used to check if a Unicode code point is a whitespace character.
Example
public class IsWhitespaceCodePointExample {
public static void main(String[] args) {
int spaceCodePoint = ' ';
int nonSpaceCodePoint = 'A';
boolean isWhitespace1 = Character.isWhitespace(spaceCodePoint);
boolean isWhitespace2 = Character.isWhitespace(nonSpaceCodePoint);
System.out.println("Is code point ' ' a whitespace character? " + isWhitespace1);
System.out.println("Is code point 'A' a whitespace character? " + isWhitespace2);
}
}
Output:
Is code point ' ' a whitespace character? true
Is code point 'A' a whitespace character? false
Handling Non-Whitespace Characters
The isWhitespace() method returns false for characters that are not whitespace characters.
Example
public class NonWhitespaceCharacterExample {
public static void main(String[] args) {
char digitChar = '1';
char specialChar = '$';
boolean isWhitespace1 = Character.isWhitespace(digitChar);
boolean isWhitespace2 = Character.isWhitespace(specialChar);
System.out.println("Is '1' a whitespace character? " + isWhitespace1);
System.out.println("Is '$' a whitespace character? " + isWhitespace2);
}
}
Output:
Is '1' a whitespace character? false
Is '$' a whitespace character? false
Checking Multiple Characters
You can check multiple characters to determine which ones are whitespace characters.
Example
public class MultipleCharactersExample {
public static void main(String[] args) {
char[] chars = { ' ', '\t', '\n', 'A', '1', '$' };
for (char ch : chars) {
boolean isWhitespace = Character.isWhitespace(ch);
System.out.printf("Is '%c' a whitespace character? %b%n", ch, isWhitespace);
}
}
}
Output:
Is ' ' a whitespace character? true
Is ' ' a whitespace character? true
Is '
' a whitespace character? true
Is 'A' a whitespace character? false
Is '1' a whitespace character? false
Is '$' a whitespace character? false
Real-World Use Case
Filtering Out Whitespace Characters
In a real-world application, you might need to filter out whitespace characters from a text string.
Example
public class FilterWhitespaceCharactersExample {
public static void main(String[] args) {
String text = "Hello\tWorld\n!";
StringBuilder filteredText = new StringBuilder();
for (char ch : text.toCharArray()) {
if (!Character.isWhitespace(ch)) {
filteredText.append(ch);
}
}
System.out.println("Filtered text: " + filteredText.toString());
}
}
Output:
Filtered text: HelloWorld!
In this example, the code removes the whitespace characters from the string.
Conclusion
The Character.isWhitespace() method in Java is a simple and effective way to check if a character is a whitespace character. By understanding how to use this method and its overloaded versions, you can efficiently handle text processing tasks that involve checking for whitespace characters in your Java applications. Whether you are validating individual characters, handling Unicode code points, or processing entire strings, the isWhitespace() method provides 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