10

Possible Duplicate:
How do I replace multiple spaces with a single space in C#?

What is the most elegant way how to trim whitespace in strings like " a<many spaces>b c " into "a b c". So, repeated whitespace is shrunk into one space.


  • @naveen, the question you link to doesn't handle the space around the string. - Lucero
  • @Lucero: there is always .Trim() for that. :) - naveen
  • @naveen, the point is that it is different. If one knows how to use a regex and trim, then the question would not even be necessary. - Lucero
  • @Lucero It's not at all different - Cristian Toma

9 답변


12

You could use Regex for this:

Regex.Replace(my_string, @"\s+", " ").Trim();


  • Ok, so why would you give -1 the answer is ok ... it can easely be fixed ... there's no point - Cristian Toma
  • @TomaCristian: Because there has been several other correct answers. Silly how everyone goes and edit theirs... Pretty lame IMO. Just delete it if you got it wrong the first time. - leppie
  • @Lucero: but when you compare your RegEx with this one I wonder if a simple Trim() isn't better. And then I mean better for readability and maintainability. - Henk Holterman
  • @leppie The main point of this site is to HELP OTHERS and second to make a reputation for yourself .. if people are editing their answers is only to help the one in need .. if you think like a reputation hunter .. that's your problem ! - Cristian Toma
  • @HenkHolterman, some regexes get messy, but this one isn't. It's easy enough to be read and understand with little regex knowledge. Also, since the regex is internally converted to an efficient automata its performance should be comparable to the "simpler" regex for collapsing spaces, and you can also have the regex compiled for even better performance. - Lucero

14

A solution w/o regex, just to have it on the table:

char[] delimiters = new char[] { ' '};   // or null for 'all whitespace'
string[] parts = txt.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
string result = String.Join(" ", parts);


  • Unlike the regex solutions using \s this will only remove space (U+0020) characters, not other characters classified as whitespace. This meets to the Q's criterion but in practice I would expect any such need to also apply to tabs etc. - Richard
  • @Richard: the OP can fine-tune that with the delimiters var. And using delimiters = null corresponds to \s - Henk Holterman
  • I always wonder (like in this question here) why complex, relatively expensive string operations are preferred by many over concise regular expressions. My motto is to use the right tools for the job to be done, and regex was the tool designed exactly for "simple" pattern matching of text regular expressions. (Henk, don't get me wrong, I'm not criticizing your answer at all, but I don't understand the adversion of many to learn regexes and to use them where they're "at home" - note that I'd not create a full parser or number validator with regexes either, they have their clear limits.) - Lucero
  • @Lucero totally true. People must have a regex-phobia or something. ionden's answer is one concise line and has fewer upvotes than these 3 ugly lines.... - Peter C
  • @alpha123, or mine with half the upvotes of Henk's, which purposely doesn't add Trim() into the mix but uses the regex to do it all. (Not that I care much, I capped the 200 reps for today several hours ago...) - Lucero

8

Regex.Replace(my_string, @"^\s+|\s+$|(\s)\s+", "$1");


  • +1 I like it. But I wonder how many of my junior programmers will understand it. :) - naveen
  • Knowing some regex basics is an important skill IMHO, especially since it is also in the toolset of the client-side web developer (where many juniors will be doing things). So if they don't understand it, they just have to look it up... it's not too complex to be dissected by a regex newbie IMHO. - Lucero
  • Sure, it can be figured out, but it takes some time to mentally parse, even with regex knowledge. The regex in the version with .Trim() is so much more concise, it's easier to maintain. (I'm not downvoting, though - this version may be preferred in some situations, like a tight loop, if it really is faster) - Izkata
  • @downvoter, please do explain yourself! - Lucero

4

Use the Trim method to remove whitespace from the beginning and end of the string, and a regular expression to reduce the multiple spaces:

s = Regex.Replace(s.Trim(), @"\s{2,}", " ");


2

You can do a

Regex.Replace(str, "\\s+", " ").Trim()

http://msdn.microsoft.com/en-us/library/e7f5w83z.aspx


0

        Regex rgx = new Regex("\\s+");
        string str;
        str=Console.ReadLine();
        str=rgx.Replace(str," ");
        str = str.Trim();
        Console.WriteLine(str);


0

Use a regular expression:

"( ){2,}" //Matches any sequence of spaces, with at least 2 of them

and use that to replace all matches with " ".

I haven't done it in C# and I need more time to figure out what the documentation says, so you'll have to find that by yourself.. sorry.


0

use regex

String test = " a    b c ";
test = Regex.Replace(test,@"\s{2,}"," ");
test = test.Trim();

this code replace any 2 or more spaces with one space using Regex then remove in the beginning and the end.


  • Your regex actually only replaces exactly two whitespaces with one. It should be \s{2,} or \s\s+ or even \s+ if you want to replace any number of whitespace by a space (note that since \s does match all whitespace including characters such as tabs the result will be different). - Lucero
  • @Lucero you are right but it will work because it will select each 2 spaces as one group so if we have 4 white spaces that means we have to groups to be replaced - Amir Ismail
  • The fix is now working, but before 2 groups would have been replaced to two spaces in the output (the output doesn't get re-processed as input). - Lucero

0

Regex.Replace(str, "[\s]+"," ")

Then call Trim to get rid of leading and trailing white space.

Linked


Related

Latest