比较字符串数组 [英] Comparing string arrays
本文介绍了比较字符串数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好,我有一个代码来比较两个字符串数组s1和s2,如下所示:
internal string[] Compare(string[] s1, string[] s2) { string[] r = new string[1], rh = new string[1]; Int32 a = 1; foreach (string s in s1) { bool b = false; foreach (string sr in s2) { if (s == sr) { b = true; } } if (b == true) { if (a.Equals(1) == true) { r = new string[a]; r[a - 1] = s; rh = r; a = a + 1; } else { r = new string[a]; Int32 bl = 0; while (bl <= rh.Length - 1) { r[bl] = rh[bl]; bl = bl + 1; } r[a - 1] = s; a = a + 1; } } b = false; } return r; }
我称它为string[] a = Compare(s1,s2)
和a
为空(s1和s2不为空)
为什么返回的结果为null?
解决方案
您似乎没有return语句.甚至不应该编译.
结果不为null.如果两个输入数组中的所有字符串都不匹配,则a
中的第一个值将为null.例如,这将生成一个大小为1的数组,其中第一个元素不为null:
字符串 [] s = Compare(新 " a", b"}, string [] { a", d"});
另外,您的某些代码很奇怪.例如,您应该只对整数使用相等运算符,而不要使用等于"方法.
那么,您在这里有什么问题?
>
Hello, I have a code to compare two string arrays s1 and s2 like this:
internal string[] Compare(string[] s1, string[] s2) { string[] r = new string[1], rh = new string[1]; Int32 a = 1; foreach (string s in s1) { bool b = false; foreach (string sr in s2) { if (s == sr) { b = true; } } if (b == true) { if (a.Equals(1) == true) { r = new string[a]; r[a - 1] = s; rh = r; a = a + 1; } else { r = new string[a]; Int32 bl = 0; while (bl <= rh.Length - 1) { r[bl] = rh[bl]; bl = bl + 1; } r[a - 1] = s; a = a + 1; } } b = false; } return r; }
I call it like string[] a = Compare(s1,s2)
and a
is null (s1 and s2 are not null)
Why the returned result is null?
解决方案
You don''t appear to have a return statement. This should not even compile.
The result isn''t null. The first value ina
will be null if none of the strings in the two input arrays match. For example, this produces an array of size 1 where the first element is not null:
string[] s = Compare(new string[] { "a", "b" }, new string[] { "a", "d" });
Also, some of your code is strange. For example, you should just use the equality operator rather than the "Equals" method on an integer.
So, what is your question here?
这篇关于比较字符串数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文