Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在C#字符串中引用字母字符_C#_Regex_If Statement_Foreach - Fatal编程技术网

在C#字符串中引用字母字符

在C#字符串中引用字母字符,c#,regex,if-statement,foreach,C#,Regex,If Statement,Foreach,我有一个字符串,我试图删除任何字符,不是字母。在下面的foreach循环中,我想知道是否有更好的方法来引用字母字符,而不必键入每个字母,也不必使用regex方法 foreach(char c in phrase) { if (!(c == 'a' || c == 'b' || c == 'c' || c == 'd' || c == 'e' || c == 'f' || c == 'g' || c == 'h' || c == 'i' || c == 'j' ||

我有一个字符串,我试图删除任何字符,不是字母。在下面的foreach循环中,我想知道是否有更好的方法来引用字母字符,而不必键入每个字母,也不必使用regex方法

foreach(char c in phrase) {
    if (!(c == 'a' || c == 'b' || c == 'c' || c == 'd' || c == 'e' || c == 'f' || c == 'g' || c == 'h' || c == 'i' ||
            c == 'j' || c == 'k' || c == 'l' || c == 'm' || c == 'n' || c == 'o' || c == 'p' || c == 'q' || c == 'r' || c == 's' ||
            c == 't' || c == 'u' || c == 'v' || c == 'w' || c == 'x' || c == 'y' || c == 'z')) {
        int i = phrase.IndexOf(c);
        phrase = phrase.Remove(i, 1);
    }
}
用这种方式写出来似乎很草率,而且很耗时。 有什么建议吗?谢谢你试试这个

var alphaPhrase = new String(phrase.Where(Char.IsLetter).ToArray());
这将为您提供变量
alphaPhrase
,其中包含
短语中的所有字母表

var alphaPhrase = new String(phrase.Where(Char.IsLetter).ToArray());
这将为您提供变量
alphaPhrase
,其中包含
短语中的所有字母表

var alphaPhrase = new String(phrase.Where(Char.IsLetter).ToArray());
这将为您提供变量
alphaPhrase
,其中包含
短语中的所有字母表

var alphaPhrase = new String(phrase.Where(Char.IsLetter).ToArray());

这将为您提供变量
alphaPhrase
phrase

中的所有字母表。我刚刚尝试测量LINQ和Regex(使用
RegexOptions.Compiled
set)方法的性能。结果表明,正则表达式更有效,但只有在您第一次运行代码时:


我刚刚尝试了测量LINQ和Regex(使用
RegexOptions.Compiled
set)方法的性能。结果表明,正则表达式更有效,但只有在您第一次运行代码时:


我刚刚尝试了测量LINQ和Regex(使用
RegexOptions.Compiled
set)方法的性能。结果表明,正则表达式更有效,但只有在您第一次运行代码时:


我刚刚尝试了测量LINQ和Regex(使用
RegexOptions.Compiled
set)方法的性能。结果表明,正则表达式更有效,但只有在您第一次运行代码时:


不使用正则表达式方法
为什么标记
正则表达式
?如果您决定仍然使用正则表达式方法:
s.Replace(新正则表达式(“[^a-z]”),string.Empty)
(区分大小写)
不使用regex方法
为什么标记
regex
?如果您决定仍然使用regex方法:
s.Replace(新regex(“[^a-z]”),string.Empty)
(区分大小写)
不使用regex方法
为什么标记
regex
?如果您决定仍然使用regex方法:
s.Replace(新regex(“[^a-z]”),string.Empty)
(区分大小写)
不使用regex方法
为什么标记
regex
?如果您决定仍然使用regex方法:
s.Replace(新regex(“[^a-z]”),string.Empty)(区分大小写)