I have strings like this:
"This______is_a____string."
(The "_" symbolizes spaces.)
I want to turn all the multiple spaces into only one. Are there any functions in C# that can do this?
Here's a nice way without regex. With Linq.
var astring = "This           is      a       string  with     to     many   spaces.";
astring = string.Join(" ", astring.Split(' ').Where(m => m != string.Empty));
output "This is a string with to many spaces"
The regex examples on this page are probably good but here is a solution without regex:
string myString = "This   is a  string.";
string myNewString = "";
char previousChar = ' ';
foreach(char c in myString)
{
  if (!(previousChar == ' ' && c == ' '))
    myNewString += c;
  previousChar = c;
}
StringBuilder, or the like, would have made my example more difficult to understand. Giving myNewString and previousChar an initial value is also not optimised, but I'm merely trying to make a suggestion as to how to approach the issue without regex. Feel free to make it "perfect" :)