I have a string from which I have to remove following char: \'\\r\', \'\\n\', and \'\\t\'. I have tried three different ways of removing these char and benchmarked them so I
Even faster:
public static string RemoveMultipleWhiteSpaces(string s)
{
char[] sResultChars = new char[s.Length];
bool isWhiteSpace = false;
int sResultCharsIndex = 0;
for (int i = 0; i < s.Length; i++)
{
if (s[i] == ' ')
{
if (!isWhiteSpace)
{
sResultChars[sResultCharsIndex] = s[i];
sResultCharsIndex++;
isWhiteSpace = true;
}
}
else
{
sResultChars[sResultCharsIndex] = s[i];
sResultCharsIndex++;
isWhiteSpace = false;
}
}
return new string(sResultChars, 0, sResultCharsIndex);
}