【发布时间】:2014-10-19 16:00:26
【问题描述】:
我正在编写一个程序来检查一个单词是否是回文。程序本身有效 但是当我输入这行特定的文本“太糟糕了 - 我隐藏了一个靴子”时,它不会拿起它 并忽略空格和连字符。
我查看了以前的问题,但找不到答案。
我的Regex.Replace 不正确吗?
string s, revs = "";
Console.WriteLine("Please Enter Word");
s = Console.ReadLine();
string r = Regex.Replace(s , @"[^-\s]", "");
for (int i = s.Length-1; i >= 0; i--)
{
revs += s[i].ToString();
}
if (revs == s)
{
Console.WriteLine("Entered string is palindrome \n String was {0} and reverse string was {1}", s, revs);
}
else
{
Console.WriteLine("String entered was not palindrome.");
}
Console.ReadKey();
【问题讨论】:
-
你尝试调试了吗?应用替换后的结果是什么?
-
它只是拾取那行文本而不是回文。好像没什么区别。
-
我说的是for循环前有断点的结果。
-
为什么在 string.Replace 上使用可读性较低的正则表达式?
标签: c# regex string palindrome