Jump to content
  • Advertisement
Sign in to follow this  
Promit

[.net] Easiest way to remove all whitespace from a string?

This topic is 4817 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Simple question. I have a string which could have newlines, spaces, tabs, or any other kind of crazy whitespace characters. What's the easiest way to get rid of all of it? (C# if that matters.)

Share this post


Link to post
Share on other sites
Advertisement
Try this:

string inputString = /* data */;
char[] charsToRemove = new char[]{'\n', '\r', '\t', ' '};

foreach (char c in charsToRemove)
{
inputString = inputString.Replace(c.ToString(), "");
}


If you need to remove a specific pattern or variable sequence, you might want to look into using Regex.

NB: This method is not optimal, since it requires n passes through the string (where n is the size of charsToRemove). I think it is optimal for the number of string-string comparisons that must be made, however (n * s, where s is the length of inputString).

Share this post


Link to post
Share on other sites
Quote:

NB: This method is not optimal.

Optimal and still fitting the requirement of removing all whitespace would be:

string = "";


Hehe. Illco.

Share this post


Link to post
Share on other sites
By non-optimal he means it requires several passes to complete. Also, that method is not memory friendly (it will require a new string to be allocated for every pass).

The proper solution would either to use a StringBuilder and make a single pass, copying non-whitespace characters to the stringbuilder, or make two passes - the first pass counts the number of white space characters, a string of the correct length (original - whitespace) is created, and a second pass copies the non-whitespace characters into the correctly sized string.

The second method is a little harder and not as clean as the stringbuilder, but it is the fastest.

(we Delphi programmers have known the gotcha behind automagical string allocation and resizing for some time)

Share this post


Link to post
Share on other sites
On second thought, there's an even easier way. This is optimal for speed, though it's not an in-place transformation.

char[] charsToRemove = new char[]{'\r', '\t', '\n', ' '};
string[] results = inputString.Split(charsToRemove);
StringBuilder transformedString = new StringBuilder();

foreach (string s in results)
{
transformedString.Append(s);
}

return transformedString.ToString();

Share this post


Link to post
Share on other sites
Quote:
Original post by kSquared
On second thought, there's an even easier way. This is optimal for speed, though it's not an in-place transformation.

char[] charsToRemove = new char[]{'\r', '\t', '\n', ' '};
string[] results = inputString.Split(charsToRemove);
StringBuilder transformedString = new StringBuilder();

foreach (string s in results)
{
transformedString.Append(s);
}

return transformedString.ToString();


Have you profiled that? Given the likely input, that Split would cause several hundred allocations.

Share this post


Link to post
Share on other sites
Quote:
Original post by Michalson
Have you profiled that? Given the likely input, that Split would cause several hundred allocations.

No, I haven't. But how do you know what the "likely input" is? Promit's specification was "I have a string".

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
What about this? Boo code, but I think you'll understand.


def foo(input as string):
chars = (char('\r'), char('\t'), char('\n'), char(' '))
builder = StringBuilder()
for index in range(input.Length):
builder.Append(input[index]) unless input[index] in chars
return builder.ToString()



I believe its an O(n), n being the maximum input length.

Share this post


Link to post
Share on other sites
Ok, this is what I came up with:

StringBuilder sb = new StringBuilder( OrigString.Length );
foreach( char c in OrigString )
{
if( !char.IsWhiteSpace( c ) )
sb.Append( c );
}
string NewString = sb.ToString();







[Edited by - Promit on August 14, 2005 3:31:36 PM]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!