Solved

General algoritm for whitespace normalization

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

821 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