I need to replace wrong symbols except chars and digits in my string with dash character “-“
var myString = "this=is+/* wrong!@# string^&*(";
I use
Regex.Replace(myString, "[^0-9a-zA-Z]+", "-");
and as a result it’s “this-is—-wrong—-string—-“
but I need “this-is-wrong-string”
What should I change in my RegEx. Thanks!
Unable to reproduce:
Output:
this-is-wrong-string-So you may want to use
TrimEnd('-')to get rid of the trailing “-“, but otherwise it looks fine to me. Compare your code with my short but complete program, and if you can’t find what’s wrong, come up with a similar short but complete program which demonstrates the problem.