简体   繁体   English

忽略回文中的空白

[英]Ignoring white space in palindrome

I am writing a program to check if a word is palindrome. 我正在编写一个程序来检查单词是否是回文。 The program itself works but when I enter this specific line of text "too bad - i hid a boot" it will not pick it up and ignore the white space and hyphen. 该程序本身可以工作,但是当我输入这一行特定文本“太糟糕了-我隐藏了启动程序”时,它将不会选择并忽略空格和连字符。

I looked at previous questions but I cant find a answer. 我看了以前的问题,但找不到答案。

Is my Regex.Replace incorrect? 我的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();

Your code contains two errors: 您的代码包含两个错误:

  • This is the correct Regex condition @"(\\s|-)" yours replaces everything but the characters you want to remove 这是正确的Regex条件@"(\\s|-)"替换了您要删除的所有字符
  • As jeffdot pointed out, you're replacing (incorrectly) but then testing against the original string 正如jeffdot所指出的那样,您要替换(错误地)但要针对原始字符串进行测试

This considered, the correct code should be: 考虑到这一点,正确的代码应为:

string s, revs = "";
        Console.WriteLine("Please Enter Word");
        s = Console.ReadLine();
        string r = Regex.Replace(s , @"(\s|-)", "");

        for (int i = r.Length-1; i >= 0; i--)
        {
            revs += r[i].ToString();
        }

        if (revs == r)
        {
            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();

Except for the first WriteLine in which you should decide what to print (since revs doesn't contain spaces and the hyphen anymore). 除了第一个WriteLine之外,您应该在其中决定要打印的内容(因为revs不再包含空格和连字符了)。

Or if you want to just use String.Replace , you can use it twice with: 或者,如果您只想使用String.Replace ,则可以通过以下方式两次使用它:

s=s.Replace(' ','');
s=s.Replace('-','');

You are assigning the result of that Regex.Replace call to string r and you are never referencing it. 您正在将Regex.Replace调用的结果分配给string r ,并且从不引用它。 This is why your comparison is failing. 这就是为什么您的比较失败的原因。

To make that more clear: you are removing whitespace and referencing that product as string r but you are reversing and testing against string s only. 更清楚地说:您正在删除空格,并将该乘积引用为string r但仅对string s进行了反转和测试。

Saverio also points out that your regex isn't going to work (I didn't test that myself). Saverio还指出,您的正则表达式将无法正常工作(我自己并未对此进行测试)。

You have: 你有:

    string r = Regex.Replace(s , @"[^-\s]", "");

    for (int i = s.Length-1; i >= 0; i--)
    {
        revs += s[i].ToString();
    }

Right there you should be iterating and reversing r instead of s --because s still has all its whitespace. 就在那儿,您应该迭代和反转r而不是s因为s仍然具有所有空白。

Further, as Saverio has said, why not just replace? 而且,正如Saverio所说的,为什么不只是更换?

string r = s.Replace(" ", string.Empty).Reverse();

I haven't tried to compile that but as long as you are not using a very old version of the .NET framework that should work. 我没有尝试编译它,但前提是您没有使用应该工作的非常旧的.NET框架。 It may require that you are using System.Linq to use the Reverse() extension. 可能需要using System.Linq才能使用Reverse()扩展。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM