Share via


CompareString

A version of this page is also available for

Windows Embedded CE 6.0 R3

4/8/2010

This function compares two character strings, using the locale specified by the given identifier as the basis for the comparison.

Note

If you specify a locale with the LCID (Locale ID) parameter and that locale is not installed or available on the Windows® phone, the function fails with ERROR_INVALID_PARAMETER. To determine whether the locale is supported or not, call IsValidLocale.

Syntax

int CompareString(
  LCID Locale, 
  DWORD dwCmpFlags, 
  LPCTSTR lpString1, 
  int cchCount1, 
  LPCTSTR lpString2, 
  int cchCount2 
); 

Parameters

  • Locale
    [in] Value that specifies the locale used for the comparison. This parameter can be one of the following predefined locale identifiers. The following table shows the values this parameter can take.

    Value Description

    LOCALE_SYSTEM_DEFAULT

    The system's default locale.

    LOCALE_USER_DEFAULT

    The current user's default locale.

    This parameter can also be a locale identifier created by the MAKELCID macro.

  • dwCmpFlags
    [in] Set of flags that indicate how the function compares the two strings. By default, these flags are not set. This parameter can specify zero to get the default behavior, or it can be any combination of the following values. The following table shows the values this parameter can take.

    Value Description

    NORM_IGNORECASE

    Ignore case.

    NORM_IGNOREKANATYPE

    Do not differentiate between Hiragana and Katakana characters. Corresponding Hiragana and Katakana characters compare as equal.

    NORM_IGNORENONSPACE

    Ignore nonspacing characters.

    NORM_IGNORESYMBOLS

    Ignore symbols.

    NORM_IGNOREWIDTH

    Do not differentiate between a single-byte character and the same character as a double-byte character.

    SORT_STRINGSORT

    Treat punctuation the same as symbols.

  • lpString1
    [in] Pointer to the first string to be compared.
  • cchCount1
    [in] Size, in characters, of the string pointed to by the lpString1 parameter. If this parameter is –1, the string is assumed to be null terminated and the length is calculated automatically.
  • lpString2
    [in] Pointer to the second string to be compared.
  • cchCount2
    [in] Size, in characters, of the string pointed to by the lpString2 parameter. If this parameter is –1, the string is assumed to be null terminated and the length is calculated automatically.

Return Value

If the function succeeds, the return value is one of the following values.

Value Description

CSTR_LESS_THAN

The string pointed to by the lpString1 parameter is less in lexical value than the string pointed to by the lpString2 parameter.

CSTR_EQUAL

The string pointed to by lpString1 is equal in lexical value to the string pointed to by lpString2.

CSTR_GREATER_THAN

The string pointed to by lpString1 is greater in lexical value than the string pointed to by lpString2.

Zero indicates failure. To get extended error information, call the GetLastError function. Possible values for GetLastError include the following:

  • ERROR_INVALID_FLAGS
  • ERROR_INVALID_PARAMETER

Remarks

Notice that if the return value is CSTR_EQUAL, which is the value 2, the two strings are "equal" in the collation sense, though not necessarily identical.

To maintain the C run-time convention of comparing strings, the value 2 can be subtracted from a nonzero return value. The meaning of < 0, ==0 and > 0 is then consistent with the C run times.

If the two strings are of different lengths, they are compared up to the length of the shortest one. If they are equal to that point, then the return value will indicate that the longer string is greater. For more information about locale identifiers, see National Language support (NLS) Locale Identifiers.

Strings are compared using what is called a "string sort" technique. In a string sort, the hyphen and apostrophe are treated just like any other nonalphanumeric symbols: they come before the alphanumeric symbols.

The following list of words is sorted using a string sort.

bill's

t-ant

billet

t-aria

bills

tanya

can't

sue's

cannot

sued

cant

sues

co-op

we're

con

went

coop

were

The LCMapString function defaults to using a word sort, but uses a string sort if their caller sets the SORT_STRINGSORT flag.

The CompareString function is optimized to run at the highest speed when dwCmpFlags is set to zero or NORM_IGNORECASE, and cchCount1 and cchCount2 have the value –1.

Requirements

Header winnls.h
Library Coreloc.lib
Windows Embedded CE Windows CE 1.0 and later
Windows Mobile Windows Mobile Version 5.0 and later

See Also

Reference

GetSystemDefaultLCID
MAKELCID