Compare strings

This code shows how to compare two strings.

Private Sub CompareString()
Dim strString1 As String
Dim strString2 As String
Dim intI, intJ As Integer

strString1 = "test"
strString2 = "test"
intI = 1
intJ = 1
While strString1.Length >= intI Or strString2.Length >= intJ
'If the length of any of the string is
'greater than 0 than compare both the strings
If Mid$(strString1, intI, 1) = Mid$(strString2, intJ, 1) Then
'Compare each and every character of the string.
'If the charater matches then increment both
'intI,intJ by 1.
intI = intI + 1
intJ = intJ + 1
'If any of the character does not match
'then the strings are not equal.
MessageBox.Show("Strings are not equal")
Exit Sub
End If
End While
MessageBox.Show("Strings are equal")
End Sub


Author: Mohan16 Nov 2009 Member Level: Gold   Points : 1

This is enough Buddy.

Dim str As String = "test"
Dim str2 As String = "test"
Dim st As Integer = StrComp(str, str2)

If st=0 Then
MessageBox.Show("Strings are equal")
MessageBox.Show("Strings are not equal")
End if

Author: krishnavenikaladi20 Nov 2009 Member Level: Gold   Points : 1


yes mohan is correct.
so simple & cute...code by mohan

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: