Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 236
  • Last Modified:

How to remove redundant blanks using .NET?

How can I remove redundant blanks in a string?  To clarify, if there is one, or more, adjancent blanks between characters, leave one blank.

Thank you.
0
Tom Sage
Asked:
Tom Sage
  • 2
1 Solution
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Lots of ways to do it...here is one:
        Dim str As String = "  some   spaces   are here      in this   string    "
        Debug.Print(str)
 
        While str.IndexOf("  ") <> -1
            str = str.Replace("  ", " ")
        End While
        str = str.Trim
        Debug.Print(str)

Open in new window

0
 
Tom SageAuthor Commented:
Idle_Mind,

Thanks for the answer.  I was trying to do this in Regex, but could never get it to work.

Thanks
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Here is a RegEx example:

        Dim str As String = "  some   spaces   are here      in this   string    "
        Debug.Print(str)
 
        str = System.Text.RegularExpressions.Regex.Replace(str, "\s+", " ").Trim
        Debug.Print(str)

Open in new window

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.

Join & Write a Comment

Featured Post

What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

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