Solved

General algoritm for whitespace normalization

Posted on 2003-11-23
2
318 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
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

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…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

813 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

12 Experts available now in Live!

Get 1:1 Help Now