代码之家  ›  专栏  ›  技术社区  ›  c00000fd

是否有一个函数/WinAPI以区分大小写的语言方式判断一个字符串是否以另一个字符串开头?

  •  0
  • c00000fd  · 技术社区  · 6 年前

    说明我的问题的最好方法是用这个例子(如果我使用 strstr CRT功能):

    const wchar_t* s1 = L"Hauptstraße ist die längste";
    const wchar_t* s2 = L"Hauptstrasse";
    
    bool b_s1_starts_with_s2 = !!wcsstr(s1, s2);
    _ASSERT(b_s1_starts_with_s2);   //Should be true
    

    到目前为止,唯一能够识别语言字符串等价性的WinAPI是 CompareStringEx 当与 LINGUISTIC_IGNORECASE s2 不断重复,直到我走到尽头。

    所以我想知道是否有更好的方法(在Windows下)来实现这一点?

    编辑: 我的意思是:

    bool b_s1_starts_with_s2 = false;
    
    int ln1 = (int)wcslen(s1);
    int ln2 = (int)wcslen(s2);
    
    for(int p = 1; p <= ln1; p++)
    {
        if(::CompareString(LOCALE_USER_DEFAULT, LINGUISTIC_IGNORECASE,
            s1, p,
            s2, ln2) == CSTR_EQUAL)
        {
            //Match
            b_s1_starts_with_s2 = true;
            break;
        }
    }
    
    2 回复  |  直到 6 年前
        1
  •  1
  •   Barmak Shemirani    6 年前

    你可以用 FindNLSString

    很明显,这是对的 ß ss

    const wchar_t *s1 = L"Hauptstraße ist die längste";
    const wchar_t *s2 = L"Hauptstrasse";
    
    INT found = 0;
    int start = FindNLSString(0, LINGUISTIC_IGNORECASE, s1, -1, s2, -1, &found);
    wprintf(L"start = %d\n", start);
    
    s1 = L"δεθ Testing Greek";
    s2 = L"ΔΕΘ";
    start = FindNLSString(0, LINGUISTIC_IGNORECASE, s1, -1, s2, -1, &found);
    wprintf(L"start = %d\n", start);
    
        2
  •  0
  •   jamesdlin    6 年前

    LCMapStringEx 将所有字符串适当地转换为小写,然后使用 wcsncmp

    (如评论中所述,您使用 wcsstr 在你的例子中 wcsstr 确定一个字符串是否包含另一个字符串。要确定一个字符串是否以另一个字符串开头,使用 使用前缀字符串的长度。)