How do I removed blank space blocks with just one blank space?

I have a string that contains blocks of blank spaces. I need to remove the blocks and replace with a single blank space.

Example:

varString = "keyword1 keyword2        keyword3                               keyword4"

I know how to do this, but I would love to see a much simpler solution.


Thanks
crnghtzAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

 
ZeonFlashCommented:
"I know how to do this, but I would love to see a much simpler solution."

If we don't know what your previously known solution is, how can we prescribe a better one? :)

Here's a way using a regular expression:

Dim varString As String = "keyword1 keyword2        keyword3                               keyword4"
MsgBox(System.Text.RegularExpressions.Regex.Replace(varString, "[ ]{2,}", " "))
0

Experts Exchange Solution brought to you by ConnectWise

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
 
MikkkCommented:
In VB code could be: (in .NET will be easily translated)

changeon=""
while len(changeon) <> len(the_text)
    changeon = Replace(the_text, "  ", " ")
wend
0
 
crnghtzAuthor Commented:
Thank you both. Sorry, I have to give the measly 90 points to ZeonFlash for answering first.

<<If we don't know what your previously known solution is, how can we prescribe a better one? :)>>

Yeah, I know. :) I just knew you guys could do it better.

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.