Solved

Compare Strings

Posted on 2001-08-08
11
535 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
[X]
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
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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
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…
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…
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…

719 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