Skip to content

Instantly share code, notes, and snippets.

@kristofclaes
Created October 2, 2012 13:05
Show Gist options
  • Save kristofclaes/3818942 to your computer and use it in GitHub Desktop.
Save kristofclaes/3818942 to your computer and use it in GitHub Desktop.
Is there a better way to replace multiple dashes with one dash?
string stringToClean = "a-string----with--too-many-------dashes";
while (stringToClean.Contains("--"))
{
stringToClean = stringToClean.Replace("--", "-");
}
@kristofclaes
Copy link
Author

Silly me... A simple regex does the trick...

string cleanedString = Regex.Replace(stringToClean, "--+");

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment