Link to home
Start Free TrialLog in
Avatar of oxygen_728
oxygen_728

asked on

VB.NET - Regex - Need to replace all occurances of Multiple Space Chars with one space

Hi.

I need to replace multiple consecutive space characters with a single space character.

For example:

"A    B     C      " becomes:  "A B C "

"   A  B C                 D"   becomes  " A B C D"

Any tips?

Thanks
ASKER CERTIFIED SOLUTION
Avatar of josgood
josgood
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
try this:
Dim TestString as String = "A    B     C      "
TestString = TestString.Replace("  ", " ")
 
Dim TestrString2 as String = "   A  B C                 D"
TestString2 = TestString2.Replace("  "," ")

Open in new window

Avatar of oxygen_728
oxygen_728

ASKER

Here's what I came up with that uses josgood's solution

StrippedResults = System.Text.RegularExpressions.Regex.Replace(StrippedResults, "([ |\n|\t|\f][ |\n|\t|\f]+)|([\n]{1})", " ")

It does the following:
        'This replaces all multiple occurances of spaces,tabs,line breaks, form feeds with a single space
        'It also replaces single line breaks with a space