vb.net 如何比较两个字符串数组?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31390196/
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
How to compare two arrays of string?
提问by bill
I want to compare two string arrays. I don't know how to implement this in vb.net.
我想比较两个字符串数组。我不知道如何在 vb.net 中实现它。
Let's say I have two arrays
假设我有两个数组
Dim A() As String = {"Hello", "How", "Are", "You?")
Dim B() As String = {"You", "How", "Something Else", "You")
And I want to compare A() with B() to see how they are similar and different (The order of the elements does not matter, which means the program does not check the order of the elements and only compares if there are same elements). Also the code would ignore the same elements in the same string array(like the second "You" in array B(). Then the code would return the elements these are same and the elements those are different. In this case the output message should be:
我想将 A() 与 B() 进行比较以查看它们的相似之处和不同之处(元素的顺序无关紧要,这意味着程序不检查元素的顺序,只比较是否有相同的元素)。此外,代码将忽略同一个字符串数组中的相同元素(如数组 B( 中的第二个“你”)。然后代码将返回这些相同的元素和不同的元素。在这种情况下,输出消息应该是:
The same elements are "You" and "How"
相同的元素是“你”和“如何”
The different element is "Something Else"
不同的元素是“别的东西”
回答by Shar1er80
With Linquse Except()to find the differences between the two arrays and Intersect()to find which elements are in both arrays.
与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
Results:
结果:
A elements not in B: Hello, Are, You?
B elements not in A: You, Something Else
Elements in both A & B: How
回答by T.S.
Use Linq extensions. Exceptwill tell you which items are different
使用 Linq 扩展。Except会告诉你哪些项目是不同的
' 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()
And you can use Jointo find same items
Here is One method to find same items, or different items
这是查找相同项目或不同项目的一种方法
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))
And of course, you can use Intersect
当然,你可以使用 Intersect
回答by Tanner W
Set up a for loop and use .equals to compare each string at each element in the arrays. I can write the code if you need it.
设置一个 for 循环并使用 .equals 来比较数组中每个元素处的每个字符串。需要的话我可以写代码。

