I would like to replace all special characters in a string with a comma (,)
.
我想用逗号(,)替换字符串中的所有特殊字符。
For Example:
例如:
Hello@Hello&Hello(Hello)
the output -
输出-
Hello,Hello,Hello,Hello,
(I don't known how to use regexp in C#)
(我不知道如何在c#中使用regexp)
Can i do this work using regexp in C#?
我可以在c#中使用regexp完成这项工作吗?
4 个解决方案
#1
47
Yes, you can use regular expressions
in C#.
是的,您可以在c#中使用正则表达式。
Using regular expressions with C#
:
使用正则表达式与c#:
using System.Text.RegularExpressions;
string your_String = "Hello@Hello&Hello(Hello)";
string my_String = Regex.Replace(your_String, @"[^0-9a-zA-Z]+", ",");
#2
9
Assume you want to replace symbols which are not digits or letters (and _ character as @Guffa correctly pointed):
假设您想要替换不是数字或字母的符号(并且_字符为@Guffa正确指向):
string input = "Hello@Hello&Hello(Hello)";
string result = Regex.Replace(input, @"[^\w\d]", ",");
// Hello,Hello,Hello,Hello,
You can add another symbols which should not be replaced. E.g. if you want white space symbols to stay, then just add \s
to pattern: \[^\w\d\s]
您可以添加另一个不应该被替换的符号。例如:如果你想要空白符号留下来,然后添加\ s模式:\[^ \ w \ d \ s]
#3
7
You can use a regular expresion to for example replace all non-alphanumeric characters with commas:
您可以使用常规的expresion,例如用逗号替换所有非字母数字字符:
s = Regex.Replace(s, "[^0-9A-Za-z]+", ",");
Note: The +
after the set will make it replace each group of non-alphanumeric characters with a comma. If you want to replace each character with a comma, just remove the +
.
注意:设置后的+将使它用逗号替换每组非字母数字字符。如果要用逗号替换每个字符,只需删除+。
#4
3
Also, It can be done with LINQ
同样,它可以用LINQ来完成。
var str = "Hello@Hello&Hello(Hello)";
var characters = str.Select(c => char.IsLetter(c) ? c : ',')).ToArray();
var output = new string(characters);
Console.WriteLine(output);
#1
47
Yes, you can use regular expressions
in C#.
是的,您可以在c#中使用正则表达式。
Using regular expressions with C#
:
使用正则表达式与c#:
using System.Text.RegularExpressions;
string your_String = "Hello@Hello&Hello(Hello)";
string my_String = Regex.Replace(your_String, @"[^0-9a-zA-Z]+", ",");
#2
9
Assume you want to replace symbols which are not digits or letters (and _ character as @Guffa correctly pointed):
假设您想要替换不是数字或字母的符号(并且_字符为@Guffa正确指向):
string input = "Hello@Hello&Hello(Hello)";
string result = Regex.Replace(input, @"[^\w\d]", ",");
// Hello,Hello,Hello,Hello,
You can add another symbols which should not be replaced. E.g. if you want white space symbols to stay, then just add \s
to pattern: \[^\w\d\s]
您可以添加另一个不应该被替换的符号。例如:如果你想要空白符号留下来,然后添加\ s模式:\[^ \ w \ d \ s]
#3
7
You can use a regular expresion to for example replace all non-alphanumeric characters with commas:
您可以使用常规的expresion,例如用逗号替换所有非字母数字字符:
s = Regex.Replace(s, "[^0-9A-Za-z]+", ",");
Note: The +
after the set will make it replace each group of non-alphanumeric characters with a comma. If you want to replace each character with a comma, just remove the +
.
注意:设置后的+将使它用逗号替换每组非字母数字字符。如果要用逗号替换每个字符,只需删除+。
#4
3
Also, It can be done with LINQ
同样,它可以用LINQ来完成。
var str = "Hello@Hello&Hello(Hello)";
var characters = str.Select(c => char.IsLetter(c) ? c : ',')).ToArray();
var output = new string(characters);
Console.WriteLine(output);