Solved

General algoritm for whitespace normalization

Posted on 2003-11-23
2
324 Views
Last Modified: 2010-04-16
Hi all. I'm often having a need for removing whitespace in strings the following way:

1. Trim left and right
2. Replace all tabs (0x9) and linebreaks (0xA + 0xD) with spaces (0x20)
3. Turn all sequences of spaces to just one space

The third step is always a problem. The solutions I've been able to come up with are very cumbersome. But this must be a common problem, so I hope there's a general algoritm for this. Show me, please.
0
Comment
Question by:liljegren
[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 Comments
 
LVL 48

Accepted Solution

by:
AlexFM earned 250 total points
ID: 9805668
Pseudo-code:

Input: string 1
Output: string 2 (initially empty)

For each character in string1
{
    if ( character != space  or  character number == 0  or  previous character != space )
    {
        add character to string2
    }
}

return string2
0
 
LVL 10

Expert Comment

by:ptmcomp
ID: 9805700
Use regular expressions. They are very strong in text matching and manipulating.

string result = Regex.Replace(input, @"((?<=\S)(?<1>(\s))\s*(?=\S))|(\s*)", "$1",  RegexOptions.Multiline | RegexOptions.ExplicitCapture);

Matches every first whitespace of whitespaces which have a non whitespace to the left and to the right.
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

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

617 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