Remove whitespace from a string in C#?
Any one know how to remove excessive whitespace from a string?
For example:
string myString = "I am going to work.";
开发者_C百科
Anyone know a good script for doing this kind of trimming?
Regex.Replace(myString, @"\s+", " ")
would do it.
Regex.Replace(myString, @"\s+", " ")
ought to do it.
I used to have:
while (myString.IndexOf(" ") >= 0)
myString = myString.Replace(" ", " ");
Probably there's more elegant way, but it works.
I prefer using Regular expressions, but this is just another suggestion.
string.Join(" ", myString.Split(' ').Where(p => p != string.Empty));
Or
string.Join(" ", myString.Split(new[] {' '}
, StringSplitOptions.RemoveEmptyEntries));
Good luck!
Different methods and which is the fastest if you have 100000 iterations to do.
Code:
Stopwatch sw = new Stopwatch();
var maxIterations = 100000;
Console.WriteLine(@"Removing Whitespace from string: "" 1m ir1 11 22 11 a3 bc 9"" with Total {0}x iterations ", maxIterations);
Console.WriteLine("\nReplace Operations");
sw.Start();
var str = " 1m ir1 11 22 11 a3 bc 9";
for (int i = 1; i <= maxIterations; i++)
{
str = " 1m ir1 11 22 11 a3 bc 9";
str = str.Replace(" ", ""); ;
}
sw.Stop();
Console.WriteLine("Finalstring: " + str);
Console.WriteLine("Elapsed time: " + sw.Elapsed.TotalMilliseconds + " Milliseconds");
sw.Reset();
//list for and if
Console.WriteLine("\nList Operations:");
sw.Start();
var str2 = " 1m ir1 11 22 11 a3 bc 9";
var listOfchars = new List<char>();
for (int i = 1; i <= maxIterations; i++)
{
str2 = " 1m ir1 11 22 11 a3 bc 9";
for (int j = 0; j < str2.Length; j++)
{
if (!(char.IsWhiteSpace(str2[j])))
listOfchars.Add(str2[j]);
}
str2 = new string(listOfchars.ToArray());
listOfchars.Clear();
}
sw.Stop();
Console.WriteLine("Finalstring: " + str2);
Console.WriteLine("Elapsed time: " + sw.Elapsed.TotalMilliseconds + " Milliseconds");
sw.Reset();
//LINQ
Console.WriteLine("\nLINQ Operations");
sw.Start();
var str1 = " 1m ir1 11 22 11 a3 bc 9";
for (int i = 1; i <= maxIterations; i++)
{
str1 = " 1m ir1 11 22 11 a3 bc 9";
str1 = String.Concat(str1.Where(c => !Char.IsWhiteSpace(c))); ;
}
sw.Stop();
Console.WriteLine("Finalstring: " + str1);
Console.WriteLine("Elapsed time: " + sw.Elapsed.TotalMilliseconds + " Milliseconds");
//Regex
sw.Reset();
Console.WriteLine("\nRegex Operations");
sw.Start();
var str3 = " 1m ir1 11 22 11 a3 bc 9";
for (int i = 1; i <= maxIterations; i++)
{
str3 = " 1m ir1 11 22 11 a3 bc 9";
str3 = Regex.Replace(str3, @"\s+", "");
}
sw.Stop();
Console.WriteLine("Finalstring: " + str3);
Console.WriteLine("Elapsed time: " + sw.Elapsed.TotalMilliseconds + " Milliseconds");
Here are the Results:
Removing Whitespace from string: " 1m ir1 11 22 11 a3 bc 9" with Total 100000x iterations
Replace Operations
Finalstring: 1mir1112211a3bc9
Elapsed time: 12,394 Milliseconds
List Operations:
Finalstring: 1mir1112211a3bc9
Elapsed time: 29,8071 Milliseconds
LINQ Operations
Finalstring: 1mir1112211a3bc9
Elapsed time: 62,175 Milliseconds
Regex Operations
Finalstring: 1mir1112211a3bc9
Elapsed time: 271,7953 Milliseconds
精彩评论