C# 比较忽略换行符和空格的两个字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11689248/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 18:51:20  来源:igfitidea点击:

Compare two strings ignoring new line characters and white spaces

c#.netstring

提问by Daniel Pe?alba

I need to compare two strings ignoring whitespaces and newline characters, so the following strings should be equal:

我需要比较忽略空格和换行符的两个字符串,因此以下字符串应该相等:

"Initial directory structure.\r\n    \r\n    The directory tree has been changed"
"Initial directory structure.\n\nThe directory tree has been changed"

Someone knows how to implement it? Thanks in advance.

有人知道如何实施吗?提前致谢。

采纳答案by jim tollan

how about:

怎么样:

string stringOne = "ThE    OlYmpics 2012!";
string stringTwo = "THe\r\n        OlympiCs 2012!";

string fixedStringOne = Regex.Replace(stringOne, @"\s+", String.Empty);
string fixedStringTwo = Regex.Replace(stringTwo, @"\s+", String.Empty);

bool isEqual = String.Equals(fixedStringOne, fixedStringTwo,
                              StringComparison.OrdinalIgnoreCase);

Console.WriteLine(isEqual);
Console.Read();

回答by musefan

How about:

怎么样:

string compareA = a.Replace(Environment.NewLine, "").Replace(" ", "");
string compareB = b.Replace(Environment.NewLine, "").Replace(" ", "");

Then you can compare both.

然后你可以比较两者。

Maybe throw that in to a helper function:

也许把它扔到一个辅助函数中:

public bool SpacelessCompare(string a, string b){
    string compareA = a.Replace(Environment.NewLine, "").Replace(" ", "");
    string compareB = b.Replace(Environment.NewLine, "").Replace(" ", "");

    return compareA == compareB;
}

回答by Joshua

copy the string then

然后复制字符串

xyz.Replace(" ", string.Empty);
xyz.Replace("\n", string.Empty);

回答by x06265616e

myString = myString.Replace("", "Initial directory structure.\r\n    \r\n    The directory tree has been changed");

Now just check if they are equal.

现在只需检查它们是否相等。

String.Compare(myString, "Initial directory structure.The directory tree has been changed")

回答by Dan Bailiff

An alternative approach is to use the CompareOptions of String.Compare.

另一种方法是使用 String.Compare 的 CompareOptions。

CompareOptions.IgnoreSymbols

CompareOptions.IgnoreSymbols

Indicates that the string comparison must ignore symbols, such as white-space characters, punctuation, currency symbols, the percent sign, mathematical symbols, the ampersand, and so on.

指示字符串比较必须忽略符号,例如空格字符、标点符号、货币符号、百分号、数学符号、与号等。

String.Compare("foo\r\n   ", "foo", CompareOptions.IgnoreSymbols);

https://docs.microsoft.com/en-us/dotnet/api/system.globalization.compareoptions

https://docs.microsoft.com/en-us/dotnet/api/system.globalization.compareoptions

回答by prussaq

Another variant, flexible (adding any skip chars), no modifying original strings or creating new ones.

另一个变体,灵活(添加任何跳过字符),无需修改原始字符串或创建新字符串。

string str = "Hel lo world!";
string rts = "Hello\n   world!";
char[] skips = { ' ', '\n' };

if (CompareExcept(str, rts, skips))
    Console.WriteLine("The strings are equal.");
else
    Console.WriteLine("The strings are not equal.");


static bool CompareExcept(string str, string rts, char[] skips)
{
    if (str == null && rts == null) return true;
    if (str == null || rts == null) return false;

    var strReader = new StringReader(str);
    var rtsReader = new StringReader(rts);
    char nonchar = char.MaxValue;

    Predicate<char> skiper = delegate (char chr)
    {
        foreach (var skp in skips)
        {
            if (skp == chr) return true;
        }
        return false;
    };

    while (true)
    {
        char a = strReader.GetCharExcept(skiper);
        char b = rtsReader.GetCharExcept(skiper);

        if (a == b)
        {
            if (a == nonchar) return true;
            else continue;
        }
        else return false;
    }
}

class StringReader : System.IO.StringReader
{
    public StringReader(string str) : base(str) { }

    public char GetCharExcept(Predicate<char> compare)
    {
        char ch;

        while (compare(ch = (char)Read())) { }

        return ch;
    }
}