Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to remove redundant blanks using .NET?

Posted on 2008-09-29
3
Medium Priority
?
220 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:Tom Sage
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 2000 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:Tom Sage
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 86

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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

604 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