Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.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_String_Palindrome - Fatal编程技术网

C# 忽略回文中的空白

C# 忽略回文中的空白,c#,regex,string,palindrome,C#,Regex,String,Palindrome,我正在写一个程序来检查一个单词是否有回文。这个程序本身是有效的 但当我输入这一行文字“太糟糕了-我藏了一只靴子”时,它不会把它捡起来 忽略空格和连字符 我看了前面的问题,但找不到答案 我的Regex.Replace是否不正确 string s, revs = ""; Console.WriteLine("Please Enter Word"); s = Console.ReadLine(); string r = Regex.Replace(s

我正在写一个程序来检查一个单词是否有回文。这个程序本身是有效的 但当我输入这一行文字“太糟糕了-我藏了一只靴子”时,它不会把它捡起来 忽略空格和连字符

我看了前面的问题,但找不到答案

我的
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();

您的代码包含两个错误:

  • 这是正确的正则表达式条件“
    @”(\s|-)”
    您的正则表达式条件将替换除要删除的字符以外的所有字符
  • 正如jeffdot指出的,您正在(错误地)替换,但随后针对原始字符串进行测试
考虑到这一点,正确的代码应为:

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();
除了第一个
WriteLine
,您应该在其中决定打印什么(因为revs不再包含空格和连字符)

或者,如果您只想使用
字符串。替换
,您可以使用它两次:

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

您正在将该
Regex.Replace
调用的结果分配给
string r
,并且您从未引用过它。这就是你的比较失败的原因

为了更清楚地说明这一点:您正在删除空格并将该产品引用为
string r
,但您正在反转并仅针对
string s
进行测试

萨维里奥还指出,你的正则表达式是行不通的(我自己没有测试过)

你有:

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

    for (int i = s.Length-1; i >= 0; i--)
    {
        revs += s[i].ToString();
    }
在这里,您应该迭代并反转
r
而不是
s
——因为
s
仍然有所有的空白

此外,正如萨维里奥所说,为什么不干脆更换

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

我还没有试着编译它,但只要你没有使用一个非常旧的.NET框架版本就可以了。可能需要您使用System.Linq
才能使用
Reverse()
扩展。

是否尝试过调试?应用替换后的结果是什么?它只是将该行文本拾取为非回文。似乎没有什么区别。我说的是在for循环之前带有断点的结果。为什么在字符串上使用可读性较差的正则表达式。替换?是的,我现在明白你的意思,但我如何引用字符串r?虽然jeff说你没有引用修改过的字符串是正确的,但你的正则表达式也是错误的,正如我在回答中指出的,你看到了吗,在将空格替换为字符串r之后,你在哪里为字符串s建立索引?我会在我的答案中添加一些例子,但这应该是比较清楚的。谢谢你们的帮助。我现在明白了我错在哪里,也明白了我应该如何引用r。