Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Compare Strings

Posted on 2001-08-08
Last Modified: 2012-08-14
How would I compare two strings similar to the 'strcmp' function in 'C'.  Code please
Question by:ff_axel
LVL 43

Expert Comment

ID: 6363892
Using the strcomp function in VB:

Returns a Variant (Integer) indicating the result of astring comparison.


StrComp(string1, string2[, compare])

The StrComp function syntax has thesenamed arguments:

Part Description
string1 Required. Any validstring expression.
string2 Required. Any valid string expression.
compare Optional. Specifies the type of string comparison. If the compareargument isNull, an error occurs. If compare is omitted, the Option Compare setting determines the type of comparison.


The compare argument settings are:

Constant Value Description
vbUseCompareOption -1 Performs a comparison using the setting of the Option Compare statement.
vbBinaryCompare 0 Performs a binary comparison.
vbTextCompare 1 Performs a textual comparison.
vbDatabaseCompare 2 Microsoft Access only. Performs a comparison based on information in your database.

Return Values

The StrComp function has the following return values:

If StrComp returns
string1 is less than string2 -1
string1 is equal to string2 0
string1 is greater than string2 1
string1 or string2 is Null Null


Expert Comment

ID: 6363900

Private Sub Command1_Click()
If Trim("Text1") = Trim("Text2") Then
    MsgBox "Same"
    MsgBox "Not Same"
End If
End Sub

Expert Comment

ID: 6363904
Try strComp

This example uses the StrComp function to return the results of a string comparison. If the third argument is 1, a textual comparison is performed; if the third argument is 0 or omitted, a binary comparison is performed.

Dim MyStr1, MyStr2, MyComp
MyStr1 = "ABCD": MyStr2 = "abcd"   ' Define variables.
MyComp = StrComp(MyStr1, MyStr2, 1)   ' Returns 0.
MyComp = StrComp(MyStr1, MyStr2, 0)   ' Returns -1.
MyComp = StrComp(MyStr2, MyStr1)   ' Returns 1.

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.


Expert Comment

ID: 6363907
The only thing I can think of would be to lcase the strings before comparison. Like:

if Lcase(strOne) > Lcase(StrTwo) Then

Author Comment

ID: 6364067
Sorry Folks, slight error in the way I asked the question.  What I'm looking for is the difference between two strings that get compared.  The strings I'm comparing are similar except for a few characters.  
LVL 15

Expert Comment

ID: 6364204
Hi axel,

           Be clear before asking questions. Post exactly what u want.

LVL 44

Expert Comment

ID: 6364237
so how would you want to see the result if the two strings were "ABCD" and "ABCE"?
LVL 43

Accepted Solution

TimCottee earned 50 total points
ID: 6364248

Private Sub Command1_Click()
  Msgbox Difference("MyString","mystring")
End Sub

Private Function Difference(ByVal String1 As String,ByVal String2 As String) As String
  Dim strMessage As String
  Dim intPos As Integer
  If Len(String1) <> Len(String2) Then
    Difference = "The strings are of different lengths"
    Exit Function
  End If
  For intPos = 1 To Len(String1)
    If Mid(String1,intPos,1) <> Mid(String2,intPos,1) Then
      strMessage = strMessage & "Position " & CStr(intPos) & " : " & Mid(String1,intPos,1) & " <> " & Mid(String2,intPos,1) & vbLF
    End If
  Difference = strMessage
End Function

Expert Comment

ID: 6364284

I put it in an array:

Option Base 1
Private Sub Form_Load()
a = "ubre"
b = "uore"
l = Len(a)
Dim r$()
ReDim r(l)
For i = 1 To l
    If Mid$(a, i, 1) <> Mid$(b, i, 1) Then r(i) = Mid$(a, i, 1)
MsgBox r(2)
End Sub



Expert Comment

ID: 6364387
>How would I compare two strings similar to the 'strcmp'
>function in 'C'.  Code please

*HOW* does the strcmp differ from StrComp? As I understand it, strcmp will return either zero (0), <0, or >0, depending upon whether the two strings are equal, the first one is less than the second (alphabetically), or the first is greater than the second (alphabetically).

Unless I'm missing something, StrComp does the very same thing.

-Dennis Borg

Author Comment

ID: 6364716
Sorry about the confusion.
Cheers, Axel

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question