Lets say I have string like this,
String sample = "This is a sample string with more than two spaces in a string ";
Now what I have to do to make the string to have only one space between each word.
Thanks in advance.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If literally mean “space” then you can replace
/ {2,}/with a single space. Note that a space in the regular expression matches a space in the text.If by “space” you actually mean “all whitespace” (spaces, tabs, newlines, etc.), then use
\s+instead.