comparing strings in vb

前端 未结 6 820
南方客
南方客 2021-01-17 08:06

Hopefully this should be an easy question. In java i think it\'s compareTo().

How do I compare 2 string variables to determine if they are the same?

6条回答
  •  余生分开走
    2021-01-17 08:38

    In vb.net you can actually compare strings with =. Even though String is a reference type, in vb.net = on String has been redefined to do a case-sensitive comparison of contents of the two strings.

    You can test this with the following code. Note that I have taken one of the values from user input to ensure that the compiler cannot use the same reference for the two variables like the Java compiler would if variables were defined from the same string Literal. Run the program, type "This" and press .

    Sub Main()
        Dim a As String = New String("This")
        Dim b As String
    
        b = Console.ReadLine()
    
        If a = b Then
            Console.WriteLine("They are equal")
        Else
            Console.WriteLine("Not equal")
        End If
        Console.ReadLine()
    End Sub
    

提交回复
热议问题