Function

GLib-2.0GLibstrncasecmpDeprecated 2.2

  • A case-insensitive string comparison, corresponding to the standard strncasecmp() function on platforms which support it. It is similar to GLib.strcasecmp except it only compares the first n characters of the strings.

    Parameters

    • Deprecateds1: string

      string to compare with s2

    • Deprecateds2: string

      string to compare with s1

    • Deprecatedn: number

      the maximum number of characters to compare

    Returns number

    0 if the strings match, a negative value if s1 < s2, or a positive value if s1 > s2

    since 2.2: The problem with g_strncasecmp() is that it does the comparison by calling toupper()/tolower(). These functions are locale-specific and operate on single bytes. However, it is impossible to handle things correctly from an internationalization standpoint by operating on bytes, since characters may be multibyte. Thus g_strncasecmp() is broken if your string is guaranteed to be ASCII, since it is locale-sensitive, and it's broken if your string is localized, since it doesn't work on many encodings at all, including UTF-8, EUC-JP, etc. There are therefore two replacement techniques: GLib.ascii_strncasecmp, which only works on ASCII and is not locale-sensitive, and GLib.utf8_casefold followed by strcmp() on the resulting strings, which is good for case-insensitive sorting of UTF-8.