Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/16.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
Arrays 如何比较两个字符串数组?_Arrays_Vb.net_String - Fatal编程技术网

Arrays 如何比较两个字符串数组?

Arrays 如何比较两个字符串数组?,arrays,vb.net,string,Arrays,Vb.net,String,我想比较两个字符串数组。我不知道如何在vb.net中实现这一点 假设我有两个数组 Dim A() As String = {"Hello", "How", "Are", "You?") Dim B() As String = {"You", "How", "Something Else", "You") 我想比较A()和B(),看看它们有什么相似之处和不同之处(元素的顺序无关紧要,这意味着程序不检查元素的顺序,只在存在相同元素时进行比较)。 此外,代码将忽略相同字符串数组中的相同元素(如数组B

我想比较两个字符串数组。我不知道如何在vb.net中实现这一点

假设我有两个数组

Dim A() As String = {"Hello", "How", "Are", "You?")
Dim B() As String = {"You", "How", "Something Else", "You")
我想比较A()和B(),看看它们有什么相似之处和不同之处(元素的顺序无关紧要,这意味着程序不检查元素的顺序,只在存在相同元素时进行比较)。 此外,代码将忽略相同字符串数组中的相同元素(如数组B()中的第二个“You”)。然后,代码将返回相同的元素和不同的元素。在这种情况下,输出消息应为:

相同的元素是“你”和“如何”


不同的元素是“其他东西”

设置for循环并使用。等于比较数组中每个元素的每个字符串。如果需要,我可以编写代码。

使用Linq扩展。
除外将告诉您哪些项不同

' ones in A but not in B
Dim result() As String = A.Except(B).ToArray()

' ones in B but not in A
Dim result() As String = B.Except(A).ToArray()

下面是一种查找相同项或不同项的方法

Dim a() as string = {"a", "b", "c"}
Dim b() as string = {"a", "b", "z"}

Dim sameItems() as String = a.Where(Function(i) b.Contains(i)).ToArray()
Array.ForEach(sameItems, Sub(i) Console.WriteLine(i))    

Dim diffItems() as String = a.Where(Function(i) Not b.Contains(i)).ToArray()
Array.foreach(diffItems, Sub(i) Console.WriteLine(i))    

当然,您可以使用
Intersect
Linq
使用
Except()
查找两个数组之间的差异,并使用
Intersect()
查找两个数组中的元素

Imports System.Linq

Module Module1
    Sub Main()
        Dim A() As String = {"Hello", "How", "Are", "You?"}
        Dim B() As String = {"You", "How", "Something Else", "You"}

        Console.WriteLine("A elements not in B: " + String.Join(", ", A.Except(B)))
        Console.WriteLine("B elements not in A: " + String.Join(", ", B.Except(A)))
        Console.WriteLine("Elements in both A & B: " + String.Join(", ", A.Intersect(B)))

        Console.ReadLine()
    End Sub
End Module
结果:

A elements not in B: Hello, Are, You?
B elements not in A: You, Something Else
Elements in both A & B: How

你希望“你”和“你”是一样的吗?难道“你好”不是一个不同的元素吗?你尝试过什么吗?你被困在哪里了?缺少
.ToArray
..
String.Join(,”,a.Except(B.ToArray)
…依我看,最好的解决方案是Shar1er80。与
String.Join(,“,a.Intersect(B.ToArray)相同)
@nelek您不需要字符串中的ToArray().Join().Except()返回一个IEnumerable,该字符串是.Join()可以使用的。我在没有
的情况下得到了这个错误。ToArray
无法将类型为“d_8b'1[System.String]”的对象强制转换为类型为“System.String[]”的对象。
..:(我使用了完整的代码)(通过这种方式,一切正常,但w/o.toArray不是)
Dim A()作为String={“Hello”、“How”、“Are”、“You”}Dim B()作为String={“Are”、“You”、“Something”}lblInfo.Text=“Same element=”+String.Join(“,”,A.Intersect(B).toArray)+“
”lblInfo.Text+=“Diff.element=”+String.Join(“,”,A.Except(B).ToArray)+“,”+String.Join(“,”,B.Except(A).ToArray)
@nelek那么您的案例中需要ToArray()。