2014-01-27 202 views
0

在程序中是2个带有字符串字段的结构。 一是结构: 用连接字符串比较两个字符串数组VB.NET

Private Structure A 
     Dim str1 as String 
     Dim str2 as String 

二级结构:

Private Structure B 
     Dim str1 as String 

地方在代码中,我把它分配给的

Dim a() as A 
Dim b() as B 

阵列例如在我们两个字符串有姓名 B的名字和姓氏在一个 结果我需要比较str1和str2的组成()与str1 b()。 在CPP这就像

for(int i = 0; i < sizeA; i++) 
{ 
    for(int j = 0; j < sizeB;i++) 
    { 
     if(!(strcmp(strcat(a[i].str1,a[i].str2),b[j].str1)) printf("GOOD!"); 
    } 
} 
+0

而你的问题是? –

+0

它实际上不会是这样,因为'strcmp'具有一个实际有用的返回值。调用'strcmp'而不检查它的返回值是毫无意义的。 –

+0

你想实现什么? –

回答

1

这是你的cpp的代码转换为VB,一些次优化:

For i = 0 To (a.Length - 1) 
    Dim strA = String.Join(" ", a(i).str1, a(i).str2) 

    For j = 0 To (b.Length - 1) 
     Dim strB = b(j).str1 

     If String.Equals(strA, strB, StringComparison.Ordinal) Then 
      Console.WriteLine("GOOD") 
     End If 

    Next 
Next 
+0

如何添加一个空格beetween concat? (Dim StrA) – user2185412

+0

@ user2185412查看我更新后的'Dim strA'的答案。 i).str1&“”&a(i).str2' –

+0

'Dim strA = a(i).str1,String.Concat(“”,a(i).str2)' –

0
If (A.str1 + A.str2) = B.str1 Then Debug.Print("GOOD") 
2

我不熟悉的CPP,但是,你可以使用LINQ:

Dim comparisons = From a1 In a 
        From b1 In b 
        Let a1Concat = a1.str1 & a1.str2 
        Let comp = String.Compare(a1Concat, b1.str1) 
' you can enumerate the query with a For-Each ' 
For Each result In comparisons 
    Dim a1Str1 As String = result.a1.str1 
    Dim a1Str2 As String = result.a1.str2 
    Dim a1Concat As String = result.a1Concat 
    Dim b1Str1 As String = result.b1.str1 
    Dim comparison As Int32 = result.comp 
Next 
+0

o_O我的头脑被炸毁了。 o_O我试图理解它))) – user2185412

+0

如果你只想使用_“GOOD”_,你可以使用'Where comp = 0'。 –

+0

根据[准则](http://msdn.microsoft.com/en-us/library/dd465121(v = vs.110).aspx),您不应该使用Compare来检查是否相等。 –