6

I have a string which contains multiple items separated by commas :

 string RESULT =  "D_CA,     P_AMOUNT    ,D_SH,D_CU,D_TO,D_GO,D_LE,D_NU,D_CO,D_MU,D_PMU,D_DP,    P_COMMENT      ";

As you can see some elements contain Whitespaces, My object is to remove all the whitespaces, This is my code:

    RESULT.Split(',').ToList().ForEach(p =>
        if (p.Contains(" "))
        {
               RESULT = RESULT.Replace(p, p.Trim());
        }
        });

And this is my result:

"D_CA,P_AMOUNT,D_SH,D_CU,D_TO,D_GO,D_LE,D_NU,D_CO,D_MU,D_PMU,D_DP,P_COMMENT"

it works well, But I ask if there is another more optimized way to get the same result?

3
  • 5
    why not a RESULT.Replace(" ","") ? Commented Jan 10, 2020 at 9:42
  • 2
    var splitResult = RESULT.Replace(" ", "").Split(','); Commented Jan 10, 2020 at 9:44
  • Does this answer your question? Remove whitespace from a string in C#? Commented Jan 10, 2020 at 10:12

9 Answers 9

8

Isn't what you looking for?

var noWhiteSpaces = RESULT.Replace(" ", string.Empty);
Sign up to request clarification or add additional context in comments.

1 Comment

This works with spaces only, not general whitespace characters.
8

I suppose you need the string with whitespaces removed. You could use String.Replace()

RESULT = RESULT.Replace(" ",string.Empty);

Alternatively, you could also use Regex for replace,

RESULT = Regex.Replace(RESULT,@"\s",string.Empty);

The regex approach would ensure replacement of all whitespace characters including tab,space etc

Comments

7

See the answer by Pavel Anikhouski, which checks the performance of all suggested solutions and actually shows that the simplified LINQ solution does not help performance too much - good to know :-) .

Simpler solution with LINQ:

string.Join(string.Empty, input.Where(c=>!char.IsWhiteSpace(c)));

First we filter away all whitespace characters and then we join them into a string. This has only one string allocation (to create the resulting string) and handles all kinds of whitespace characters, not only spaces.

Original answer

Use a StringBuilder to build up the resulting string and then go through the input string with a foreach, always checking char.IsWhiteSpace(character). In case the character is not whitespace, append it in the StringBuilder by calling Append(character) method. At the end just return the resulting string by calling ToString() on the StringBuilder.

var builder = new StringBuilder();
foreach(var character in input)
{
   if (!char.IsWhiteSpace(character))
   {
      builder.Append(character);
   }
}
return builder.ToString();

This implementation is more efficient, as it does not produce any string allocations, except for the end result. It just works with the input string and reads it once.

7 Comments

Isn't it a bit overkill to use a StringBuilder and loops when one can just Replace() ?
Regex.Replace(input, @"\s", string.Empty); should do the job
Regex is however slower the first time, as it needs to be parsed. I think that would hinder the performance benefit (although worth a benchmark :-) )
As the OP asked for optimized solution, I suppose the performance is primary concern. But agreed regex and LINQ solutions are more readable than the one I posted originally :-D
Actually, regex is 10 times slower, that solution using stringbuilder. Extension method from my answer above is about 30-40% slower, the solution here. That's about performance :)
|
5

You can Trim():

var split = RESULT.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
    .Select(s=> s.Trim());

Comments

4

You can use the following extension method for that

public static class Ext
{
    public static string RemoveWhitespaces(this string input)
    {
        return new string(input.ToCharArray()
            .Where(c => !char.IsWhiteSpace(c))
            .ToArray());
    }
}

Usage sample

string RESULT = "D_CA,     P_AMOUNT    ,D_SH,D_CU,D_TO,D_GO,D_LE,D_NU,D_CO,D_MU,D_PMU,D_DP,    P_COMMENT      ";
RESULT = RESULT.RemoveWhitespaces();

Char.IsWhiteSpace method indicates all Unicode whitespace characters.

There is a simple benchmark for all proposed solutions below. In terms of performance the usual Replace is fastest method, but it doesn't remove all whitespace characters. The solution with StringBuilder seems most relevant in terms of performance and functionality, it's about 2 times faster than extension above. The Regex and Linq solution are few times slower than other ones

enter image description here

3 Comments

@MartinZikmund thank you, I've added a string builder result as well
Could you check the LINQ solution too, please :-) ?
@MartinZikmund I've added details, Linq solution is expensive as well, but faster than regex
3

You can simplify the code like this:

RESULT.Replace(" ", string.Empty).Split(',');

Assuming that you need the elements in an Enumerable. Otherwise just the Replace() should work for you.

Comments

1

I can only to add that you can put the linq into the static method

public static class Utility
{
    public static string RemoveWhitespaces(this string input)
    {
        return string.Join(string.Empty, input.Where(c => !char.IsWhiteSpace(c)));
    }
}

and then use it with any your string:

Console.WriteLine("asdas asdas sa   asdasd232 sd".RemoveWhitespaces());

Comments

-1

RESULT.Replace(" ", string.Empty);

Comments

-2

You can use .Trim() to make a new string without any whitespaces.

string x = "Hello World";
console.writeline(x.Trim());
//Output : "HelloWorld"

you can check other overloads of string.Trim() here: String.Trim Method

2 Comments

The very link you provided clearly states Trim() Removes all leading and trailing white-space characters from the current string. The output of your code is "Hello World".....
Try it yourself!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.