(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Compare String value in If statement
Module Module1
Sub Main()
Dim Language As String = "English"
If (Language = "English") Then
Console.WriteLine("Hello, world!")
ElseIf (Language = "Spanish") Then
Console.WriteLine("Hola, mundo")
End If
End Sub
End Module
Hello, world!
Comparing strings: Equals, =, String.Equals() and CompareTo
Module Tester
Sub Main()
Dim string1 As String = "hello"
Dim string2 As String = "good bye"
Dim string3 As String = "Happy Birthday"
Dim string4 As String = "happy birthday"
Dim output As String
Dim quotes As Char = ChrW(34)
If (string1.Equals("hello")) Then
Console.WriteLine("string1 equals hello")
Else
Console.WriteLine("not equal ")
End If
" test for equality with =
If string1 = "hello" Then
Console.WriteLine("string1 equals " & quotes & "hello" & quotes )
Else
Console.WriteLine("string1 does not equal " & quotes & "hello" & quotes)
End If
If (String.Equals(string3, string4)) Then
Console.WriteLine("string3 equals string4")
Else
Console.WriteLine("string3 does not equal string4")
End If
" test CompareTo
Console.WriteLine("string1.rupareTo(string2) is " & _
string1.rupareTo(string2) & vbCrLf & _
"string2.rupareTo(string1) is " & _
string2.rupareTo(string1) & vbCrLf & _
"string1.rupareTo(string1) is " & _
string1.rupareTo(string1) & vbCrLf & _
"string3.rupareTo(string4) is " & _
string3.rupareTo(string4) & vbCrLf & _
"string4.rupareTo(string3) is " & _
string4.rupareTo(string3))
End Sub " Main
End Module
string1 equals hello
string1 equals "hello
string3 does not equal string4
string1.rupareTo(string2) is 1
string2.rupareTo(string1) is -1
string1.rupareTo(string1) is 0
string3.rupareTo(string4) is 1
string4.rupareTo(string3) is -1
String Compare with case sensitive and insensitive
Class Tester
Shared Sub Main( )
Dim s1 As [String] = "abcd"
Dim s2 As [String] = "ABCD"
Dim result As Integer
result = [String].rupare(s1, s2)
Console.WriteLine("compare s1: {0}, s2: {1}, result: {2}" _
& Environment.NewLine, s1, s2, result)
result = [String].rupare(s1, s2, True)
Console.WriteLine("Compare insensitive. result: {0}" _
& Environment.NewLine, result)
End Sub
End Class
compare s1: abcd, s2: ABCD, result: -1
Compare insensitive. result: 0
String Equals with overloaded operator, member method and static member method
Option Strict On
Imports System
Class Tester
Public Shared Sub Main( )
Dim s1 As String = "abcd"
Dim s2 As String = "ABCD"
" the String copy method
Dim s5 As String = String.Copy(s2)
Console.WriteLine("s5 copied from s2: {0}", s5)
" copy with the overloaded operator
Dim s6 As String = s5
Console.WriteLine("s6 = s5: {0}", s6)
" member method
Console.WriteLine("Does s6.Equals(s5)?: {0}", s6.Equals(s5))
" shared method
Console.WriteLine("Does Equals(s6,s5)?: {0}", _
String.Equals(s6, s5))
End Sub "Main
End Class "Tester
s5 copied from s2: ABCD
s6 = s5: ABCD
Does s6.Equals(s5)?: True
Does Equals(s6,s5)?: True