?
Solved

Compare Strings

Posted on 2001-08-08
11
Medium Priority
?
541 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
Independent Software Vendors: 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 200 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month9 days, 12 hours left to enroll

762 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