[英]C# Remove special characters
I want to remove all special characters from a string.我想从字符串中删除所有特殊字符。 Allowed characters are AZ (uppercase or lowercase), numbers (0-9), underscore (_), white space ( ), pecentage(%) or the dot sign (.).允许的字符为 AZ(大写或小写)、数字 (0-9)、下划线 (_)、空格 ( )、百分数 (%) 或点符号 (.)。
I have tried this:我试过这个:
StringBuilder sb = new StringBuilder();
foreach (char c in input)
{
if ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') | c == '.' || c == '_' || c == ' ' || c == '%')
{ sb.Append(c); }
}
return sb.ToString();
And this:和这个:
Regex r = new Regex("(?:[^a-z0-9% ]|(?<=['\"])s)", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant | RegexOptions.Compiled);
return r.Replace(input, String.Empty);
But nothing seems to be working.但似乎没有任何效果。 Any help will be appreciated.任何帮助将不胜感激。
Thank you!谢谢!
Regex.Replace(input, "[^a-zA-Z0-9% ._]", string.Empty)
You can simplify the first method to您可以将第一种方法简化为
StringBuilder sb = new StringBuilder();
foreach (char c in input)
{
if (Char.IsLetterOrDigit(c) || c == '.' || c == '_' || c == ' ' || c == '%')
{ sb.Append(c); }
}
return sb.ToString();
which seems to pass simple tests.这似乎通过了简单的测试。 You can shorten it using LINQ您可以使用 LINQ 缩短它
return new string(
input.Where(
c => Char.IsLetterOrDigit(c) ||
c == '.' || c == '_' || c == ' ' || c == '%')
.ToArray());
The first approach seems correct, except that you have a |
第一种方法似乎是正确的,除了您有一个|
(bitwise OR) instead of a ||
(按位或)而不是||
before c == '.'
在c == '.'
. .
By the way, you should state what doesn't work (doesn't it compile, or does it crash, or does it produce wrong output?)顺便说一句,你应该说明什么不起作用(它不编译,还是崩溃,或者它是否产生错误的输出?)
StringBuilder sb = new StringBuilder();
foreach (char c in input)
{
if (char.IsLetterOrDigit(c) || "_ %.".Contains(c.ToString()))
sb.Append(c);
}
return sb.ToString();
private string RemoveReservedCharacters(string strValue)
{
char[] ReservedChars = {'/', ':','*','?','"', '<', '>', '|'};
foreach (char strChar in ReservedChars)
{
strValue = strValue.Replace(strChar.ToString(), "");
}
return strValue;
}
This is how my version might look.这就是我的版本可能的样子。
StringBuilder sb = new StringBuilder();
foreach (char c in input)
{
if (Char.IsLetterOrDigit(c) ||
c == '.' || c == '_' || c == ' ' || c == '%')
sb.Append(c);
}
}
return sb.ToString();
Cast each char to an int, then compare its ascii code to the ascii table, which you can find all over the internet: http://www.asciitable.com/将每个字符转换为 int,然后将其 ascii 代码与 ascii 表进行比较,您可以在互联网上找到该表: http : //www.asciitable.com/
{
char[] input = txtInput.Text.ToCharArray();
StringBuilder sbResult = new StringBuilder();
foreach (char c in input)
{
int asciiCode = (int)c;
if (
//Space
asciiCode == 32
||
// Period (.)
asciiCode == 46
||
// Percentage Sign (%)
asciiCode == 37
||
// Underscore
asciiCode == 95
||
( //0-9,
asciiCode >= 48
&& asciiCode <= 57
)
||
( //A-Z
asciiCode >= 65
&& asciiCode <= 90
)
||
( //a-z
asciiCode >= 97
&& asciiCode <= 122
)
)
{
sbResult.Append(c);
}
}
txtResult.Text = sbResult.ToString();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.