Solved

Compare Strings

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

Expert Comment

by:TimCottee
ID: 6363892
Using the strcomp function in VB:

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

Syntax

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.


Settings

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

0
 

Expert Comment

by:surki
ID: 6363900
Try:

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

Expert Comment

by:kprestage
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.

0
 
LVL 1

Expert Comment

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

if Lcase(strOne) > Lcase(StrTwo) Then
0
 

Author Comment

by:ff_axel
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.  
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 15

Expert Comment

by:mohan_sekar
ID: 6364204
Hi axel,

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

Mohan
0
 
LVL 44

Expert Comment

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

Accepted Solution

by:
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
  Next
  Difference = strMessage
End Function
0
 
LVL 2

Expert Comment

by:TrueDrake
ID: 6364284
Hi,

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)
Next
MsgBox r(2)
End Sub

Enjoy

0
 
LVL 8

Expert Comment

by:DennisBorg
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
0
 

Author Comment

by:ff_axel
ID: 6364716
Sorry about the confusion.
Cheers, Axel
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

707 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now