In this short article, we look at how to remove or trim all white spaces from a given string in Java.
Let's first we'll write Java method to trim all whitespace from the given String that is trim white spaces from leading, trailing, and in between characters.
trimAllWhitespace(String str) Method
Let's write a Java method with logic to remove all white spaces.
Logical steps
- Iterate over each character of a given String.
- Check each character with white space using built-in Character.isWhitespace(c) method.
- If the character is not a white space then append a character to StringBuilder instance.
- Finally, return String via calling toString() method on StringBuilder instance.
/** * Trim <i>all</i> whitespace from the given {@code String}: * leading, trailing, and in between characters. * @param str the {@code String} to check * @return the trimmed {@code String} * @see java.lang.Character#isWhitespace */ public static String trimAllWhitespace(String str) { if (!hasLength(str)) { return str; } int len = str.length(); StringBuilder sb = new StringBuilder(str.length()); for (int i = 0; i < len; i++) { char c = str.charAt(i); if (!Character.isWhitespace(c)) { sb.append(c); } } return sb.toString(); }
Complete Java Program
Here is a complete Java program to demonstrate the above method with input value " Java Guides ".
package com.javaguides.corejava.string;
/**
* @author Ramesh Fadatare
*
*/
public class StringTrimAllWhitespace {
public static void main(String[] args) {
String str = " Java Guides ";
String result = trimAllWhitespace(str);
System.out.println(result);
}
/**
* Trim <i>all</i> whitespace from the given {@code String}:
* leading, trailing, and in between characters.
* @param str the {@code String} to check
* @return the trimmed {@code String}
* @see java.lang.Character#isWhitespace
*/
public static String trimAllWhitespace(String str) {
if (!hasLength(str)) {
return str;
}
int len = str.length();
StringBuilder sb = new StringBuilder(str.length());
for (int i = 0; i < len; i++) {
char c = str.charAt(i);
if (!Character.isWhitespace(c)) {
sb.append(c);
}
}
return sb.toString();
}
public static boolean hasLength(String str) {
return (str != null && !str.isEmpty());
}
}
Output:
JavaGuides
Related String Programs
- Java program to Count Number of Duplicate Words in String
- Java Program to Count Number of Words in Given String
- Java Program to Count the Number of Occurrences of Substring in a String
- Java Program to Count the Occurrences of Each Character in String
- Java Program to Merge two String Arrays
- Java Program to Remove Duplicate Words from String
- Java Program to Reverse a String(5 ways)
- Java Program to Reverse Each Word of a String
- Java Program to Swap Two Strings
- How to Check if the String Contains only Digits
- How to Check if the String Contains only Letters
- How to Check If the String Contains Only Letters or Digits
- Java Program to Check if Input String is Palindrome
- Java Program to Find all Permutations of String
- How to Remove Leading and Trailing White Space From a String in Java
Free Spring Boot Tutorial | Full In-depth Course | Learn Spring Boot in 10 Hours
Watch this course on YouTube at Spring Boot Tutorial | Fee 10 Hours Full Course