Removing Whitespace from Strings in Java
I Have a String Like This: Mysz = "Name=John Age=13 Year=2001"; I Want to Remove the Whitespaces in the String. I Tried Trim() but This Removes Only...
I have a string like this:
mysz = "name=john age=13 year=2001";
I want to remove the whitespaces in the string. I tried trim() but this removes only whitespaces before and after the whole string. I also tried replaceAll("\\W", "") but then the = also gets removed.
How can I achieve a string with:
mysz2 = "name=johnage=13year=2001"
37 Answers
st.replaceAll("\\s+","") removes all whitespaces and non-visible characters (e.g., tab, \n).
st.replaceAll("\\s+","") and st.replaceAll("\\s","") produce the same result.
The second regex is 20% faster than the first one, but as the number consecutive spaces increases, the first one performs better than the second one.
Assign the value to a variable, if not used directly:
st = st.replaceAll("\\s+","")
replaceAll("\\s","")
\w = Anything that is a word character
\W = Anything that isn't a word character (including punctuation etc)
\s = Anything that is a space character (including space, tab characters etc)
\S = Anything that isn't a space character (including both letters and numbers, as well as punctuation etc)
(Edit: As pointed out, you need to escape the backslash if you want \s to reach the regex engine, resulting in \\s.)
The most correct answer to the question is:
String mysz2 = mysz.replaceAll("\\s","");
I just adapted this code from the other answers. I'm posting it because besides being exactly what the question requested, it also demonstrates that the result is returned as a new string, the original string is not modified as some of the answers sort of imply.
(Experienced Java developers might say "of course, you can't actually modify a String", but the target audience for this question may well not know this.)
How about replaceAll("\\s", ""). Refer here.
One way to handle String manipulations is StringUtils from Apache commons.
String withoutWhitespace = StringUtils.deleteWhitespace(whitespaces);
You can find it here. commons-lang includes lots more and is well supported.
If you need to remove unbreakable spaces too, you can upgrade your code like this :
st.replaceAll("[\\s|\\u00A0]+", "");
You should use
s.replaceAll("\\s+", "");
instead of:
s.replaceAll("\\s", "");
This way, it will work with more than one spaces between each string. The + sign in the above regex means "one or more \s"
--\s = Anything that is a space character (including space, tab characters etc). Why do we need s+ here?
If you prefer utility classes to regexes, there is a method trimAllWhitespace(String) in StringUtils in the Spring Framework.
You've already got the correct answer from Gursel Koca but I believe that there's a good chance that this is not what you really want to do. How about parsing the key-values instead?
import java.util.Enumeration;
import java.util.Hashtable;
class SplitIt {
public static void main(String args[]) {
String person = "name=john age=13 year=2001";
for (String p : person.split("\\s")) {
String[] keyValue = p.split("=");
System.out.println(keyValue[0] + " = " + keyValue[1]);
}
}
}
output:
name = john
age = 13
year = 2001
The easiest way to do this is by using the org.apache.commons.lang3.StringUtils class of commons-lang3 library such as "commons-lang3-3.1.jar" for example.
Use the static method "StringUtils.deleteWhitespace(String str)" on your input string & it will return you a string after removing all the white spaces from it. I tried your example string "name=john age=13 year=2001" & it returned me exactly the string that you wanted - "name=johnage=13year=2001". Hope this helps.
You can do it so simply by
String newMysz = mysz.replace(" ","");
public static void main(String[] args) {
String s = "name=john age=13 year=2001";
String t = s.replaceAll(" ", "");
System.out.println("s: " + s + ", t: " + t);
}
Output:
s: name=john age=13 year=2001, t: name=johnage=13year=2001
I am trying an aggregation answer where I test all ways of removing all whitespaces in a string. Each method is ran 1 million times and then then the average is taken. Note: Some compute will be used on summing up all the runs.
Must Read
Results:
1st place from @jahir 's answer
- StringUtils with short text: 1.21E-4 ms (121.0 ms)
- StringUtils with long text: 0.001648 ms (1648.0 ms)
2nd place
- String builder with short text: 2.48E-4 ms (248.0 ms)
- String builder with long text: 0.00566 ms (5660.0 ms)
3rd place
- Regex with short text: 8.36E-4 ms (836.0 ms)
- Regex with long text: 0.008877 ms (8877.0 ms)
4th place
- For loop with short text: 0.001666 ms (1666.0 ms)
- For loop with long text: 0.086437 ms (86437.0 ms)