Solved

How to remove redundant blanks using .NET?

Posted on 2008-09-29
3
208 Views
Last Modified: 2012-05-05
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
Comment
Question by:MisterT25
  • 2
3 Comments
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 22598408
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
 

Author Closing Comment

by:MisterT25
ID: 31501239
Idle_Mind,

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

Thanks
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 22598851
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

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question