lstrcmp Function

Declare Function lstrcmp Lib "kernel32.dll" Alias "lstrcmpA" (ByVal lpString1 As String, ByVal lpString2 As String) As Long

Platforms

Description & Usage

lstrcmp compares two strings using a case-sensitive comparison based on the current user locale. The function uses a word sort method, which places all symbols except hyphens and apostrophes before the letter "a" (hyphens and apostrophes are treated differently). The strings are compared by comparing the first character of each string, then the second character, etc., until unequal characters are encountered.

Return Value

If the function returns a negative value, the first string is less than the second string (i.e., the first string comes before the second string in alphabetical order). If the function returns 0, the two strings are equal. If the function returns a positive value, the first string is greater than the second string. In any case, the actual return value is the difference of the first unequal characters encountered.

Visual Basic-Specific Issues

None.

Parameters

lpString1
The first string to compare.
lpString2
The second string to compare.

Example

' This code is licensed according to the terms and conditions listed here.

' Use a case-senitive comparison method to alphabetically sort
' nine words.  The sorting method simply compares each possible pair
' of words; if a pair is out of alphabetical order, they are switched.
Dim words(1 To 9) As String  ' the words to sort
Dim tempstr As String  ' buffer used to swap strings
Dim oc As Integer, ic As Integer  ' counter variables
Dim compval As Long  ' result of comparison

' Load the nine strings into the array.
words(1) = "can't"
words(2) = "cant"
words(3) = "cannot"
words(4) = "pants"
words(5) = "co-op"
words(6) = "coop"
words(7) = "Denver"
words(8) = "denver"
words(9) = "denveR"

' Sort the strings, swapping any pairs which are out of order.
For oc = 1 To 8  ' first string of the pair
  For ic = oc + 1 To 9  ' second string of the pair
    ' Compare the two strings.
    compval = lstrcmp(words(oc), words(ic))
    ' If words(oc) is greater, swap them.
    If compval > 0 Then
      tempstr = words(oc)
      words(oc) = words(ic)
      words(ic) = tempstr
    End If
  Next ic
Next oc

' Display the list of sorted words.
For oc = 1 To 9
  Debug.Print words(oc)
Next oc

See Also

CompareString, lstrcmpi

Category

Strings

Go back to the alphabetical Function listing.
Go back to the Reference section index.


Last Modified: December 30, 1999
This page is copyright © 1999 Paul Kuliniewicz. Copyright Information Revised October 29, 2000
Go back to the Windows API Guide home page.
E-mail: vbapi@vbapi.com Send Encrypted E-Mail
This page is at http://www.vbapi.com/ref/l/lstrcmp.html