1. 程式人生 > 實用技巧 >Delphi WinAPI CompareString 和 CompareStringEx 比較兩個識別符號指定的兩個字串函式

Delphi WinAPI CompareString 和 CompareStringEx 比較兩個識別符號指定的兩個字串函式

Delphi WinAPI CompareString 和 CompareStringEx 比較兩個識別符號指定的兩個字串函式

1、CompareString

函式原型:

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

引數:

  • Locale
    • LOCALE_CUSTOM_DEFAULT
    • LOCALE_CUSTOM_UI_DEFAULT
    • LOCALE_CUSTOM_UNSPECIFIED
    • LOCALE_INVARIANT
    • LOCALE_SYSTEM_DEFAULT
    • LOCALE_USER_DEFAULT
  • dwCmpFlags //指示函式如何比較兩個字串的標誌。lpString1 //指向要比較的第一個字串的指標。
    • LINGUISTIC_IGNORECASE //忽略大小寫
    • LINGUISTIC_IGNOREDIACRITIC //忽略非空字元,忽略大小寫
    • NORM_IGNORECASE // Ignore case. For many scripts (notably Latin scripts), NORM_IGNORECASE coincides with LINGUISTIC_IGNORECASE.
      • Note NORM_IGNORECASE //ignores any tertiary distinction, whether it is actually linguistic case or not. For example, in Arabic and Indic scripts, this distinguishes alternate forms of a character, but the differences do not correspond to linguistic case. LINGUISTIC_IGNORECASE causes the function to ignore only actual linguistic casing, instead of ignoring the third sorting weight.
      • Note With this flag set, the function ignores the distinction between the wide and narrow forms of the CJK compatibility characters.
    • NORM_IGNOREKANATYPE //Do not differentiate between hiragana and katakana characters. Corresponding hiragana and katakana characters compare as equal.
    • NORM_IGNORENONSPACE //Ignore nonspacing characters. For many scripts (notably Latin scripts), NORM_IGNORENONSPACE coincides with LINGUISTIC_IGNOREDIACRITIC.
      • Note NORM_IGNORENONSPACE //ignores any secondary distinction, whether it is a diacritic or not. Scripts for Korean, Japanese, Chinese, and Indic languages, among others, use this distinction for purposes other than diacritics. LINGUISTIC_IGNOREDIACRITIC causes the function to ignore only actual diacritics, instead of ignoring the second sorting weight.
      • Note NORM_IGNORENONSPACE only has an effect for locales in which accented characters are sorted in a second pass from main characters. Normally all characters in the string are first compared without regard to accents and, if the strings are equal, a second pass over the strings is performed to compare accents. This flag causes the second pass to not be performed. For locales that sort accented characters in the first pass, this flag has no effect.
    • NORM_IGNORESYMBOLS //Ignore symbols and punctuation.
    • NORM_IGNOREWIDTH //Ignore the difference between half-width and full-width characters, for example, C a t == cat. The full-width form is a formatting distinction used in Chinese and Japanese scripts.
    • NORM_LINGUISTIC_CASING //Use the default linguistic rules for casing, instead of file system rules. Note that most scenarios for CompareStringEx use this flag. This flag does not have to be used when your application calls CompareStringOrdinal.
    • SORT_DIGITSASNUMBERS //Windows 7: Treat digits as numbers during sorting, for example, sort "2" before "10".
    • SORT_STRINGSORT //Treat punctuation the same as symbols.
  • cchCount1 //lpString1指示的字串長度,不包括終止的空字元。
  • lpString2 //指向要比較的第二個字串的指標。
  • cchCount2//lpString2指示的字串長度,不包括終止的空字元。

返回值:

  • 如果成功,則返回以下值之一。為了保持比較字串的C執行時慣例,可以從非零返回值中減去值2。那麼,<0,==0,and>0的含義與C執行時是一致的。
  • CSTR小於。lpString1指示的字串的詞法值小於lpString2指示的字串。
  • CSTR_相等。lpString1表示的字串在詞法值上與lpString2表示的字串等效。這兩個字串在排序時是等價的,但不一定完全相同。
  • 強制大於。lpString1指示的字串的詞法值大於lpString2指示的字串。
  • 如果函式不成功,則返回0。

2、CompareStringEx

函式原型:

int CompareStringEx(
  LPCWSTR                          lpLocaleName,
  DWORD                            dwCmpFlags,
  _In_NLS_string_(cchCount1)LPCWCH lpString1,
  int                              cchCount1,
  _In_NLS_string_(cchCount2)LPCWCH lpString2,
  int                              cchCount2,
  LPNLSVERSIONINFO                 lpVersionInformation,
  LPVOID                           lpReserved,
  LPARAM                           lParam
);

  

建立時間:2020.08.27  更新時間: