Using CultureInfo.CurrentCulture to compare strings with current locale settings

Get help with using AutoHotkey and its commands and hotkeys
User avatar
JnLlnd
Posts: 310
Joined: 29 Sep 2013, 21:29
GitHub: JnLlnd
Location: Montreal, Quebec, Canada
Contact:

Using CultureInfo.CurrentCulture to compare strings with current locale settings

23 Mar 2020, 16:00

Hi,

I've seen on StackOverflow that C# has a built-in function .NET to compare strings taking into account locale settings:

string s1 = "HEllO";
string s2 = "héLLo";

if (String.Compare(s1, s2, CultureInfo.CurrentCulture, CompareOptions.IgnoreNonSpace | CompareOptions.IgnoreCase) == 0)
{
// both strings are equal
}


Does anyone knows if this relies on this Windows .NET component that could be called using DllCall (or another method)?

Thanks.
:thumbup: Author of Quick Access Popup, the powerful Windows folders, apps and documents launcher!
:clap: Quick Access Popup Knowledge base - Join our kind sponsors
:ugeek: Courses on AutoHotkey
User avatar
Alguimist
Posts: 369
Joined: 05 Oct 2015, 16:41
Contact:

Re: Using CultureInfo.CurrentCulture to compare strings with current locale settings

24 Mar 2020, 08:18

This is probably what you are looking for:

Code: Select all

MsgBox % CompareStringEx("HEllO", "héLLo", 0x21)
/*
String Flags.
    NORM_IGNORECASE            0x00000001  // ignore case
    NORM_IGNORENONSPACE        0x00000002  // ignore nonspacing chars
    NORM_IGNORESYMBOLS         0x00000004  // ignore symbols
    LINGUISTIC_IGNORECASE      0x00000010  // linguistically appropriate 'ignore case'
    LINGUISTIC_IGNOREDIACRITIC 0x00000020  // linguistically appropriate 'ignore nonspace'
    NORM_IGNOREKANATYPE        0x00010000  // ignore kanatype
    NORM_IGNOREWIDTH           0x00020000  // ignore width
    NORM_LINGUISTIC_CASING     0x08000000  // use linguistic rules for casing

Compare String Return Values.
    CSTR_LESS_THAN             1           // string 1 less than string 2
    CSTR_EQUAL                 2           // string 1 equal to string 2
    CSTR_GREATER_THAN          3           // string 1 greater than string 2
*/

CompareStringEx(String1, String2, Flags) {
    Local

    Str1Len := StrPutVar(String1, Str1, "UTF-16")
    Str2Len := StrPutVar(String2, Str2, "UTF-16")

    Return DllCall("Kernel32.dll\CompareStringEx"
    , "Ptr",  0
    , "UInt", Flags
    , "Ptr",  &Str1, "Int", Str1Len
    , "Ptr",  &Str2, "Int", Str2Len
    , "Ptr", 0, "Ptr", 0, "Ptr", 0, "Int")
}

StrPutVar(String, ByRef Var, Encoding) {
    VarSetCapacity(Var, StrPut(String, Encoding)
    * ((Encoding = "UTF-16" || Encoding = "CP1200") ? 2 : 1), 0)
    Return StrPut(String, &Var, Encoding)
}
User avatar
JnLlnd
Posts: 310
Joined: 29 Sep 2013, 21:29
GitHub: JnLlnd
Location: Montreal, Quebec, Canada
Contact:

Re: Using CultureInfo.CurrentCulture to compare strings with current locale settings

24 Mar 2020, 10:29

This is exactly it, Alguimist ! Many thanks :-)

I'm not sure I understand the meaning of 'ignore case' and 'ignore nonspace' in

LINGUISTIC_IGNORECASE 0x00000010 // linguistically appropriate 'ignore case'
LINGUISTIC_IGNOREDIACRITIC 0x00000020 // linguistically appropriate 'ignore nonspace'

I tried to replace 0x21 with 0x11 just to see and it returned 0 (error?). And with 0x10, it returned 3 (string 1 greater than string 2).

I looked at https://docs.microsoft.com/en-us/windows/win32/api/stringapiset/nf-stringapiset-comparestringex but it did not help a lot...

Anyway, the important is that 0x21 does exactly want I need. Thanks again.
:thumbup: Author of Quick Access Popup, the powerful Windows folders, apps and documents launcher!
:clap: Quick Access Popup Knowledge base - Join our kind sponsors
:ugeek: Courses on AutoHotkey

Return to “Ask For Help”

Who is online

Users browsing this forum: Bing [Bot], boiler, Google [Bot], ktfroy, Toruk and 63 guests