If you have multiple spaces inside a string in Java, how do you condense them into a single space between words? -


if string has multiple spaces between words:

the    cat         sat            on                     mat. 

how make single space?

the cat sat on mat. 

i tried didn't work:

mytext = mytext.trim().replace("  ", " "); 

with regular expression:

mytext.trim().replaceall("\\s+", " "); 

this reads: "trim text , replace occurrences of 1 or more 1 whitespace character (including tabs, line breaks, etc) 1 single whitespace"

see java.util.regex.pattern more details on java regular expressions:

http://download.oracle.com/javase/6/docs/api/java/util/regex/pattern.html


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -