In this article, we will discuss how to remove white-spaces in a String. String can have trailing/leading white-spaces as well as white-spaces in-between String.
Remove white-spaces in a String :
- Using String.replaceAll() method
- Without using any in-built function
1. Using String.replaceAll() method :
- Using replaceAll() method of String class is the simplest to remove String by another String
- Method signature: public String replaceAll(String regex, String replacement);
- This method takes 2 input-arguments where
- 1st argument is the regex pattern or simply String to be removed
- 2nd argument is the replacement String
- Finally, trim the result to remove trailing or leading white-spaces if any before returning to the caller method
RemoveWhitespacesUsingReplaceFunction.java
package in.bench.resources.java.remove.whitespaces;
public class RemoveWhitespacesUsingReplaceFunction {
// main() method
public static void main(String[] args) {
// sample string 1
String actualStr1 = "Wel come To Java World";
System.out.println("Original String : " + actualStr1);
System.out.println("After removing white-spaces : "
+ removeAllWhitespaces(actualStr1));
// sample string 2 with leading/trailing white-spaces
String actualStr2 = " One World Happy People ";
System.out.println("\nOriginal String : " + actualStr2);
System.out.println("After removing white-spaces : "
+ removeAllWhitespaces(actualStr2));
}
/**
* This method removes all white-spaces and additionally trims
* trailing and leading white-spaces if any using replaceAll() method
*
* @param str
* @return
*/
public static String removeAllWhitespaces(String str) {
// removes all white-spaces and trims trailing/leading white-spaces
return str.replaceAll("\\s", "").trim();
}
}
Output:
Original String : Wel come To Java World
After removing white-spaces : WelcomeToJavaWorld
Original String : One World Happy People
After removing white-spaces : OneWorldHappyPeople
2. Without using in-built functions
- Here, we need to build logic to remove white-spaces by iterating through each characters in a String
- First step is to convert String into a character Array
- Second step is to iterate through each characters in an Array and check whether it contains white-spaces
- If it contains white-spaces then by-pass
- Otherwise append this character into StringBuilder using append(); method
- Third step is to convert StringBuilder into String using toString() method
- Finally, trim the result to remove trailing or leading white-spaces if any before returning to the caller method
RemoveWhitespacesWithoutUsingReplaceFunction.java
package in.bench.resources.java.remove.whitespaces;
public class RemoveWhitespacesWithoutUsingReplaceFunction {
// main() method
public static void main(String[] args) {
// sample string 1
String actualStr1 = "Wel come To Java World";
System.out.println("Original String : " + actualStr1);
System.out.println("After removing white-spaces : "
+ removeAllWhitespaces(actualStr1));
// sample string 2 with leading/trailing white-spaces
String actualStr2 = " One World Happy People ";
System.out.println("\nOriginal String : " + actualStr2);
System.out.println("After removing white-spaces : "
+ removeAllWhitespaces(actualStr2));
}
/**
* This method removes all white-spaces and additionally trims
* trailing and leading white-spaces if any by iterating char array
* and checking each characters
*
* @param str
* @return
*/
public static String removeAllWhitespaces(String str) {
// local variables
StringBuilder sbd = new StringBuilder();
// convert String into char[] Array
char[] chArray = str.toCharArray();
// iterate char[] array and store chars into sbd neglecting white-spaces
for(int index=0; index<chArray.length; index++) {
// neglect chars if it is contains white-spaces
if(chArray[index] != ' ' && chArray[index] != '\t') {
// append chars to StringBuilder
sbd.append(chArray[index]);
}
}
// removes all white-spaces and trims trailing/leading white-spaces
return sbd.toString().trim();
}
}
Output:
Original String : Wel come To Java World
After removing white-spaces : WelcomeToJavaWorld
Original String : One World Happy People
After removing white-spaces : OneWorldHappyPeople
Hope, everyone found this article useful. If anyone has suggestion to improve or some other technique to be included, then please comment.
Related Articles :
- Java – Remove duplicate characters from String in Java
- Java – How to UPPERCASE every duplicate character/occurrences in a String in Java
- Java – How to check whether 2 strings are Anagrams in Java ?
- Java – How to remove white-spaces in a String ?
- Java – How to swap 2 Strings without using Third variable ?
- Java – Count and print number of Vowels and Consonants in a String
- Java – Remove input-ted Character from a given String
- Java – How to check whether given String contains only Alphabets or Letters ?
- Java – How to check whether given String contains only Digits ?
- Java – How to check whether given String contains Alpha-numeric characters only ?
References :
Happy Coding !!
Happy Learning !!