在else条件下,数组字符串不起作用 [英] Array string is not working in if else condition

查看:76
本文介绍了在else条件下,数组字符串不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述







我在m = {price [0] .Trim()中获得价值> 100.00 }



我在n中获得价值> 100.00。






I am getting value">100.00" in m ={prices[0].Trim()}

and I am getting value ">100.00" in n.

string[] m = { prices[0].Trim() };
string[] n = new string[] {">100.00"};

if (m == n)
{
....
}
else
{
...
}







在上面的if条件为真(m == n)。但光标不进入条件,它进入其他条件。如何解决这个问题。为什么它不会如果条件。请帮帮我。谢谢。




In the above if condition is true (m == n). but cursor does not go to if condition, it goes to else condition. how to fix this. why it is not going to if condition. please help me out. Thank you.

推荐答案

数组是引用类型;所以当你写的时候

Arrays are reference types; so when you write
if (m == n)



你确实在比较引用,这是这个上下文中的两个不同的对象。



因为它们似乎只包含一个元素,你可以写:


you are indeed comparing references, which are two distinct objects in this context.

As they seems to only contain one element, you could write:

if (m[0].Equals(n[0]))





如果它们包含更多比一个元素,你必须实现自己的字符串数组比较,一个满足你的需求。



If they contain more than one element, then you have to implement your own string arrays comparison, one that meets your needs.


如果你比较2个字符串数组,你可以使用linq的SequenceEqual,检查出来:





http://www.dotnetperls .com / sequenceequal [ ^ ]
If you are comparing 2 string arrays,you can use SequenceEqual of linq, check this out:


http://www.dotnetperls.com/sequenceequal[^]


如果您使用的是.NET FrameWork 3.5或更高版本,那么可以使用Linq的SequenceEqual运算符来测试两个IEnumebles的所有元素的相等性:
If you are using .NET FrameWork 3.5 or later, you can use the Linq 'SequenceEqual operator to test for the equality of all elements of two IEnumerables:
string[] ary1 = { "1", "2", "3", "1" };
string[] ary2 = { "1", "2", "3", "1" };

// in some method:
bool isAryEqual = ary1.SequenceEqual(ary2);

如果你打算使用'SequenceEqual,我建议你研究这里的例子:[ ^ ]。

If you are going to use 'SequenceEqual, I suggest you study the examples here: [^].


这篇关于在else条件下,数组字符串不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆