Solved

How to remove redundant blanks using .NET?

Posted on 2008-09-29
3
195 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
This video teaches viewers about errors in exception handling.

762 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now