Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.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
.net 如何比较两个字符串列表以找到相同的字符串_.net_Vb.net_String_List - Fatal编程技术网

.net 如何比较两个字符串列表以找到相同的字符串

.net 如何比较两个字符串列表以找到相同的字符串,.net,vb.net,string,list,.net,Vb.net,String,List,我有两个字符串列表,其中包含一组名称,我想知道的是如何比较这两个列表以找到相同的名称,然后编写一个if语句来执行基于比较的操作 清单1: 菲利浦 上下快速移动 迈克尔 清单2: 詹姆斯 彼得 鲍勃第一次写VB。可以使用下面的嵌套循环查找相同的循环。如果您希望大小写有意义,请将equalsIgnoreCase替换为just equals Dim list1() as String ={"name1","name2"} Dim list2() as String ={"name3",

我有两个字符串列表,其中包含一组名称,我想知道的是如何比较这两个列表以找到相同的名称,然后编写一个if语句来执行基于比较的操作

清单1: 菲利浦 上下快速移动 迈克尔

清单2: 詹姆斯 彼得
鲍勃第一次写VB。可以使用下面的嵌套循环查找相同的循环。如果您希望大小写有意义,请将equalsIgnoreCase替换为just equals

    Dim list1() as String ={"name1","name2"}
    Dim list2() as String ={"name3","name2"}
    For Each str as String In list1
        For Each names as String In list2
          If String.Compare(str,names) = 0 Then
           Console.WriteLine(str+" "+names)
          End If
        Next names
    Next str
这可能会有帮助

    lstNew = lstOne.Intersect(lstTwo, StringComparer.OrdinalIgnoreCase)
    PrintList(lstNew)

    Console.ReadLine()
    End Sub

     Private Sub PrintList(ByVal str As IEnumerable(Of String))
     For Each s In str
     Console.WriteLine(s)
     Next s
     Console.WriteLine("-------------")
参考文献

许多linq扩展中的一个是
Intersect
,它返回这两个扩展共有的元素:

Dim names1 = {"Philip", "Bob", "Michael"}
Dim names2 = {"James", "Ziggy", "Bob", "Hoover"}

Dim commonNames = names1.Intersect(names2).ToArray()
For Each n As String In commonNames
    Console.WriteLine(n)
Next
输出:

鲍勃

这里有很多,你可以键入
(点),然后通过Intellisense浏览它们,阅读它们所做的事情的一行代码,至少意识到它们的存在

    Dim names1 = {"Philip", "Ziggy", "Bob", "Michael", "James"}
    Dim names2 = {"James", "Ziggy", "Bob", "Michael", "Hoover"}

    Dim commonNames = names1.Intersect(names2).ToArray()
    Dim Match As String = ""
    Dim NumberOfMatches = 0

    For Each n As String In commonNames
        Match += n.ToString + " "
        NumberOfMatches += 1
    Next

    TextBox1.Text = Match
    TextBox2.Text = NumberOfMatches
输出将包含所有相同的名称和匹配数: 齐吉·鲍勃·迈克尔·詹姆斯& 四,