Looking for quick, simple way in Java to change this string
" hello there "
to something that looks like this
"hello there"
where I replace all those multiple spaces with a single space, except I also want the one or more spaces at the beginning of string to be gone.
Something like this gets me partly there
String mytext = " hello there ";
mytext = mytext.replaceAll("( )+", " ");
but not quite.
you should do it like this
put + inside round brackets.
Use the Apache commons
StringUtils.normalizeSpace(String str)
method. See docs herereduce spaces first
capitalize the first letter and lowercase everything else
This worked for me
where filter is following function and scan is the input string:
You can first use
String.trim()
, and then apply the regex replace command on the result.This worked perfectly for me :
sValue = sValue.trim().replaceAll("\\s+", " ");