• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 153
  • Last Modified:

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
0
crnghtz
Asked:
crnghtz
1 Solution
 
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
 
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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now