【发布时间】:2015-02-26 05:35:10
【问题描述】:
我想从字符串中删除所有非字母字符。当我说所有字母时,我指的是不在字母表中的任何字母或撇号。这是我的代码。
public static string RemoveBadChars(string word)
{
char[] chars = new char[word.Length];
for (int i = 0; i < word.Length; i++)
{
char c = word[i];
if ((int)c >= 65 && (int)c <= 90)
{
chars[i] = c;
}
else if ((int)c >= 97 && (int)c <= 122)
{
chars[i] = c;
}
else if ((int)c == 44)
{
chars[i] = c;
}
}
word = new string(chars);
return word;
}
它很接近,但不太有效。问题是这样的:
[in]: "(the"
[out]: " the"
它给了我一个空格而不是“(”。我想完全删除这个字符。
【问题讨论】:
-
您有空格(空字符),因为 chars[] 中的元素如果是坏字符则为零。我认为你需要根据你有多少坏字符来缩小字符串
标签: c#