C# 比较两个字符串,忽略新行字符和空格
我需要比较两个忽略空格和换行符的字符串,因此以下字符串应该相等:C# 比较两个字符串,忽略新行字符和空格,c#,.net,string,C#,.net,String,我需要比较两个忽略空格和换行符的字符串,因此以下字符串应该相等: "Initial directory structure.\r\n \r\n The directory tree has been changed" "Initial directory structure.\n\nThe directory tree has been changed" 如何实现它?如何: string compareA = a.Replace(Enviro
"Initial directory structure.\r\n \r\n The directory tree has been changed"
"Initial directory structure.\n\nThe directory tree has been changed"
如何实现它?如何:
string compareA = a.Replace(Environment.NewLine, "").Replace(" ", "");
string compareB = b.Replace(Environment.NewLine, "").Replace(" ", "");
然后你可以比较两者
可能会将其加入到辅助函数中:
public bool SpacelessCompare(string a, string b){
string compareA = a.Replace(Environment.NewLine, "").Replace(" ", "");
string compareB = b.Replace(Environment.NewLine, "").Replace(" ", "");
return compareA == compareB;
}
然后复制字符串
xyz.Replace(" ", string.Empty);
xyz.Replace("\n", string.Empty);
现在只需检查它们是否相等
String.Compare(myString, "Initial directory structure.The directory tree has been changed")
那么:
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();
另一种方法是使用String.Compare的CompareOptions 比较选项。忽略符号 指示字符串比较必须忽略符号,例如空格字符、标点符号、货币符号、百分号、数学符号、符号等
String.Compare("foo\r\n ", "foo", CompareOptions.IgnoreSymbols);
另一种变体,灵活(添加任何跳过字符),无需修改原始字符串或创建新字符串
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;
}
}
string str=“Hel-lo-world!”;
string rts=“Hello\n world!”;
char[]跳过={'','\n'};
if(比较异常(str、rts、跳过))
WriteLine(“字符串相等”);
其他的
WriteLine(“字符串不相等”);
静态bool compareeexcept(字符串str、字符串rts、字符[]跳过)
{
如果(str==null&&rts==null)返回true;
如果(str==null | | rts==null)返回false;
var strReader=新的StringReader(str);
var rtsReader=新的StringReader(rts);
char nonchar=char.MaxValue;
谓词skiper=委托(char-chr)
{
foreach(跳过中的var skp)
{
if(skp==chr)返回true;
}
返回false;
};
while(true)
{
char a=strReader.GetCharExcept(skiper);
char b=rtsReader.GetCharExcept(skiper);
如果(a==b)
{
如果(a==nonchar)返回true;
否则继续;
}
否则返回false;
}
}
类StringReader:System.IO.StringReader
{
publicstringreader(stringstr):base(str){}
公共字符getCharException(谓词比较)
{
char ch;
while(compare(ch=(char)Read()){}
返回ch;
}
}
查看对问题投票最多的答案难道你不能从这两个字符串中删除新行和空白字符并进行比较吗?我在这里给出了一个类似问题的答案:请记住,当你去掉空格时,这两个字符串被标记为相同的:“红色上方”“那边多佛”
换行符算作空白吗?第一行没有意义。CultureInfo.CurrentCulture.CompareInfo.Compare(lhs、rhs、CompareOptions.IgnoreSymbols)
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;
}
}