Solved

How to remove redundant blanks using .NET?

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

There is an easy way, in .NET, to centralize the treatment of all unexpected errors. First of all, instead of launching the application directly in a Form, you need first to write a Sub called Main, in a module. Then, set the Startup Object to th…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

867 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

18 Experts available now in Live!

Get 1:1 Help Now