Solved

How to remove redundant blanks using .NET?

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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

737 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