Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace Single WhiteSpace without Replacing Multiple WhiteSpace

Tags:

c#

whitespace

I have a string in the format: abc def ghi xyz

I would like to end with it in format: abcdefghi xyz

What is the best way to do this? In this particular case, I could just strip off the last three characters, remove spaces, and then add them back at the end, but this won't work for cases in which the multiple spaces are in the middle of the string.

In Short, I want to remove all single whitespaces, and then replace all multiple whitespaces with a single. Each of those steps is easy enough by itself, but combining them seems a bit less straightforward.

I'm willing to use regular expressions, but I would prefer not to.

like image 743
aaron Avatar asked Dec 25 '22 09:12

aaron


2 Answers

This approach uses regular expressions but hopefully in a way that's still fairly readable. First, split your input string on multiple spaces

var pattern = @"  +"; // match two or more spaces
var groups = Regex.Split(input, pattern);

Next, remove the (individual) spaces from each token:

var tokens = groups.Select(group => group.Replace(" ", String.Empty));

Finally, join your tokens with single spaces

var result = String.Join(' ', tokens.ToArray());

This example uses a literal space character rather than 'whitespace' (which includes tabs, linefeeds, etc.) - substitute \s for ' ' if you need to split on multiple whitespace characters rather than actual spaces.

like image 135
Dylan Beattie Avatar answered May 03 '23 23:05

Dylan Beattie


Well, Regular Expressions would probably be the fastest here, but you could implement some algorithm that uses a lookahead for single spaces and then replaces multiple spaces in a loop:

// Replace all single whitespaces
for (int i = 0; i < sourceString.Length; i++)
{
    if (sourceString[i] = ' ')
    {
        if (i < sourceString.Length - 1 && sourceString[i+1] != ' ')
          sourceString = sourceString.Delete(i);
    }
}

// Replace multiple whitespaces
while (sourceString.Contains("  ")) // Two spaces here!
  sourceString = sourceString.Replace("  ", " ");

But hey, that code is pretty ugly and slow compared to a proper regular expression...

like image 31
Thorsten Dittmar Avatar answered May 04 '23 00:05

Thorsten Dittmar