Solved

Compare Strings

Posted on 2001-08-08
11
527 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
message box in access 4 52
Crystal reports - Formula Field code need assistance with code 17 92
Help me. 3 71
add criteria to query in VB, Access 2003 2 32
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

726 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