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()
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+","")