Remove Whitespace From A String

When I was interviewing at Microsoft I actually coded this in C# as if in C++ using pointers and char manipulation. Everyone knows a string is immutable. My interviewer was not impressed with my mixed language capabilities pseudocode. When I got home I threw together the different ways of doing it I mentioned to the interviewer, just to prove I knew what I meant. :) If you have a better way to do it let me know, don't let me post junk out here. These were the most common.

Back to Code Samples

public static class RemoveExtraWhitespaceFromString
{
//===================================================================================
/// <summary>
/// Removes the extra whitespace from a string using a char array.
/// <remarks>Speed rating: 1.0: This is the fastest of the methods. It theoretically should use the least amount of memory also. However it
/// is less readable than UsingCharArray2.</remarks>
/// <example>UsingCharArray("12 3 ") returns "12 3 ".</example>
/// </summary>
/// <param name="source">The source string from which to remove whitespace.</param>
/// <returns>The original string with the extra whitespace removed.</returns>
//===================================================================================
public static string UsingCharArray(string source)
{
if (String.IsNullOrEmpty(source))
return source;

int replacementPosition = -1;

char[] chars = source.ToCharArray();

for (int i = 1; i < chars.Length; i++)
{
if (chars[i] == ' ' && chars[i - 1] == ' ')
{
if (replacementPosition == -1)
{
replacementPosition = i;
}
}
else if (replacementPosition > -1)
{
chars[replacementPosition++] = chars[i];
}
}

if (replacementPosition > -1)
return new string(chars, 0, replacementPosition);
else
return new string(chars);
}

//===================================================================================
/// <summary>
/// Removes the extra whitespace from a string using a char array.
/// <remarks>Speed rating 1.07: This method is slightly slower than UsingCharArray.</remarks>
/// <example>UsingCharArray("12 3 ") returns "12 3 ".</example>
/// </summary>
/// <param name="source">The source string from which to remove whitespace.</param>
/// <returns>The original string with the extra whitespace removed.</returns>
//===================================================================================
public static string UsingCharArray2(string source)
{
if (String.IsNullOrEmpty(source))
return source;

int replacementPosition = 0;

char[] chars = source.ToCharArray();
bool inWhitespaceBlock = false;
bool charIsWhitespace;

for (int i = 0; i < chars.Length; i++)
{
charIsWhitespace = source[i] == ' ';

if (!inWhitespaceBlock || !charIsWhitespace)
{
chars[replacementPosition++] = chars[i];
}

inWhitespaceBlock = charIsWhitespace;
}

if (replacementPosition > -1)
return new string(chars, 0, replacementPosition);
else
return new string(chars);
}

//===================================================================================
/// <summary>
/// Removes the whitespace from a string using a char array instead of StringBuilder.
/// </summary>
/// <remarks>Speed rating: 1.98: This method is only slightly faster than the StringBuilder method.</remarks>
/// <param name="source">The string to remove extra whitespace from.</param>
/// <returns>The source string with extra whitespace removed.</returns>
/// <example>UsingCharArray("12 3 ") returns "12 3 ".</example>
//===================================================================================
public static string BuildingNewCharArray(string source)
{
if (String.IsNullOrEmpty(source))
return source;

List<char> newString = new List<char>(source.Length);

bool inWhitespaceBlock = false;
bool charIsWhitespace;

for (int i = 0; i < source.Length; i++)
{
charIsWhitespace = source[i] == ' ';

if (!inWhitespaceBlock || !charIsWhitespace)
{
newString.Add(source[i]);
}

inWhitespaceBlock = charIsWhitespace;
}

return new String(newString.ToArray<char>());
}

//===================================================================================
/// <summary>
/// Removes the whitespace from a string using a StringBuilder object.
/// </summary>
/// <remarks>Speed rating: 2.00: This takes twice as long as the char array method.</remarks>
/// <param name="source">The string from which to remove whitespace.</param>
/// <returns>The source string with extra whitespace removed.</returns>
//===================================================================================
public static string UsingStringBuilder(string source)
{
if (String.IsNullOrEmpty(source))
return source;

StringBuilder builder = new StringBuilder();
bool inWhitespaceBlock = false;
bool charIsWhitespace;

for (int i = 0; i < source.Length; i++)
{
charIsWhitespace = source[i] == ' ';

if (!inWhitespaceBlock || !charIsWhitespace)
{
builder.Append(source[i]);
}

inWhitespaceBlock = charIsWhitespace;
}

return builder.ToString();
}

//===================================================================================
/// <summary>
/// Removes the whitespace from a string using an uncompiled RegEx object.
/// </summary>
/// <remarks>Speed rating: 48.42: Kids, don't try this at home. I am a professional.</remarks>
/// <param name="source">The string from which to remove whitespace.</param>
/// <returns>The source string with extra whitespace removed.</returns>
//===================================================================================
public static string UsingRegEx(string source)
{
if (String.IsNullOrEmpty(source))
{
return source;
}
else
{
return new System.Text.RegularExpressions.Regex("[ ]{
2,
}").Replace(source, " ");
}
}

static Regex regEx = new Regex("[ ]{
2,
}", RegexOptions.Compiled);

//===================================================================================
/// <summary>
/// Removes the whitespace from a string using a compiled Regex object.
/// </summary>
/// <remarks>Speed rating: 2.38: Danger, Will Robinson.</remarks>
/// <param name="source">The string from which to remove whitespace.</param>
/// <returns>The source string with extra whitespace removed.</returns>
//===================================================================================
public static string UsingRegEx2(string source)
{
if (String.IsNullOrEmpty(source))
{
return source;
}
else
{
return regEx.Replace(source, " ");
}
}

//===================================================================================
/// <summary>
/// Removes the whitespace from a string using a string object's Replace method.
/// </summary>
/// <remarks>Speed rating: 1.73: Surprisingly not bad.</remarks>
/// <param name="source">The string from which to remove whitespace.</param>
/// <returns>The source string with extra whitespace removed.</returns>
//===================================================================================
public static string UsingStringReplace(string source)
{
if (String.IsNullOrEmpty(source))
{
return source;
}

string newString = source;
int lastLength;

do {
lastLength = newString.Length;
newString = newString.Replace(" ", " ");
} while (newString.Length != lastLength);

return newString;
}
}

Back to Code Samples