Java Program To Remove All Whitespaces From The Given String.
Remove whitespaces from all over the String
We can replace all whitespaces with empty string using `String.replaceAll() method.
The replace all methods accept regex string and replacement string as input params. And return the replaced string.
We can use the "\\s+"
regex expression to find all the whitespaces from the given input string.
Java Program
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/**
* A Java program to remove all the whitespaces from the given string.
* By coderolls.com
*/
public class RemoveWhitespaces {
public static void main(String[] args) {
String exampleString = "It is an example string.";
System.out.println("The example String is as given below: ");
System.out.println(exampleString);
//Remove all whitespaces from the example string
String stringWithoutWhitespaces = exampleString.replaceAll("\\s+", "");
System.out.println("\nThe example String after removing all the whitespaces is as given below: ");
System.out.println(stringWithoutWhitespaces);
}
}
Output
1
2
3
4
5
The example String is given below:
It is an example string.
The example String after removing all the whitespaces is as given below:
Itisanexamplestring.
Join Newsletter
Get the latest tutorials right in your inbox. We never spam!