Wednesday, September 20, 2023

 Remove all white spaces from String in Java



This is a common question in coding interviews for junior developers.
There are multiple ways we can remove all white spaces from String:

1. Using the built-in trim() method
This is only if we need to remove spaces from the beginning and end of the string.


public class Main {
public static void main(String[] args) {
String str =
" Hello "; // white space at beginning and end of the string
System.out.println("The length of the string with white spaces: " + str.length());
str =
removeWhiteSpaces(str);
System.
out.println("The length of the string after removing white spaces: " + str.length());
System.
out.println("Final string: " + str);
}

private static String removeWhiteSpaces(String str) {
return str.trim();
}
} Output:
The length of the string with white spaces: 7 The length of the string after removing white spaces: 5 Final string: Hello

Note: When we use any of the methods of the String class, we need to remember that we get a completely new String object, so we need to assign the return value to a variable since String is an immutable object in Java.


2. Using the built-in replaceAll() method
This method accepts a regular expression and removes all white spaces contained in the String.


public class Main {
public static void main(String[] args) {
String str = " Hello again, this is a Dev Primer blog. "; // white space at beginning, middle, and end of the string
System.out.println("The length of the string with white spaces: " + str.length());
str = removeWhiteSpaces(str);
System.out.println("The length of the string after removing white spaces: " + str.length());
System.out.println("Final string: " + str);
}

private static String removeWhiteSpaces(String str) {
// replace all white spaces (regex = "\\s") with empty string
return str.replaceAll("\\s", "");
}
} Output: The length of the string with white spaces: 41 The length of the string after removing white spaces: 32 Final string: Helloagain,thisisaDevPrimerblog.


3. Creating a new String object and checking for white space using Character.isWhitespace(char c) method.

public class Main {
public static void main(String[] args) {
String str = " Hello again, this is a Dev Primer blog. "; // white space at beginning, middle, and end of the string
System.out.println("The length of the string with white spaces: " + str.length());
str = removeWhiteSpaces(str);
System.out.println("The length of the string after removing white spaces: " + str.length());
System.out.println("Final string: " + str);
}

private static String removeWhiteSpaces(String str) {
String withoutWhiteSpaces = "";

for (int i = 0; i < str.length(); i++) {
if (!Character.isWhitespace(str.charAt(i))) {
withoutWhiteSpaces += str.charAt(i);
}
}

return withoutWhiteSpaces;
}
} Output: The length of the string with white spaces: 41 The length of the string after removing white spaces: 32 Final string: Helloagain,thisisaDevPrimerblog.

And that is how we can remove white spaces from String in Java.



No comments:

Post a Comment