Java how to replace 2 or more spaces with single space in string and delete leading spaces only

Posted by Nessa on Stack Overflow See other posts from Stack Overflow or by Nessa
Published on 2010-05-28T20:43:11Z Indexed on 2010/05/28 20:51 UTC
Read the original article Hit count: 215

Filed under:

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.

© Stack Overflow or respective owner

Related posts about java