[英]How to I replace all chars and numbers in string except several chars
how to I replace all characters and numbers in string except several chars, for example "f", "a", "l" to avoid somthing like this:如何替换字符串中除几个字符外的所有字符和数字,例如“f”、“a”、“l”以避免这样的事情:
String str = "replace different characters except several";
Console.WriteLine("Input: " + str);
str = str.Replace('a', '.').Replace('b', '.').Replace('c', '.');
Console.WriteLine("Output: " + str);
Use Regex for these kind of scenarios:在这些场景中使用正则表达式:
String str = "replace different characters except several";
str = Regex.Replace(str, @"[^fal]", "."); //Replace all with "." EXCEPT f,a,l
Console.WriteLine(str);
Output:- "...la.....ff........aa.................al"
输出:-“...la.....ff........aa....al”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.