Compare strings in Dot Net

vishalneeraj-24503
Posted by vishalneeraj-24503 under VB.NET category on | Points: 40 | Views : 952
Today, i am going to tell you about Comparing 2 strings in various ways :-

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim str1 As String = "vishal" //lower case
Dim str2 As String = "VISHAL" //upper case
Dim str3 As String = "vishal1"
Dim str4 As String = "kumar"
Dim str5 As String = "vishal"

Dim a As Boolean = If(str1 = str5, True, False)
Dim i As Integer = String.Compare(str1, str2)
Dim x As Integer = String.Compare(str2, str1)
Dim j As Integer = String.Compare(str1, str2, True)
Dim k As Integer = String.Compare(str1, str2, False)
Dim l As Integer = String.Compare(str1, str3)
Dim m As Integer = String.Compare(str1, str4)
Dim n As Integer = String.Compare(str1, str5)
Dim o As Boolean = IIf(str1.Equals(str5), True, False)
Dim p As Boolean = IIf(str1 = str5, True, False)
Dim q As Boolean = IIf(str1.Equals(str2), True, False)
Dim r As Boolean = IIf(str1 = str2, True, False)
Dim s As Boolean = IIf(str1.ToLower().Equals(str2.ToLower()), True, False)
Dim t As Boolean = IIf(str1.ToUpper() = str2.ToUpper(), True, False)
Dim u As Boolean = IIf(str1.ToLower().Equals(str2.ToUpper()), True, False)
Dim v As Integer = String.Compare(str1, str4, True)
Dim w As Integer = String.Compare(str1, str4, False)
Dim y As Integer = String.Compare(str1, str2, StringComparison.OrdinalIgnoreCase)
Dim z As Integer = String.Compare(str1, str5, StringComparison.OrdinalIgnoreCase)
End Sub

Output will be :-
a = True
i = -1
x = 1
j = 0
k = -1
l = -1
m = 1
n = 0
o = True
p = True
y = 0
z = 0
q = False
r = False
s = True
t = True
u = False
v = 1
w = 1

0 and True Means Values are Equals.

Comments or Responses

Login to post response