C++11 case insensitive comparison of beginning of a string (unicode)

后端 未结 3 465
青春惊慌失措
青春惊慌失措 2021-01-05 01:45

I have to check if the particular string begins with another one. Strings are encoded using utf8, and a comparison should be case insensitive.

I know that this is ve

3条回答
  •  太阳男子
    2021-01-05 02:34

    Using the stl regex classes you could do something like the following snippet. Unfortunately its not utf8. Changing str2 to std::wstring str2 = L"hello World" results in a lot of conversion warnings. Making str1 an std::wchar doesn't work at all, since std::regex doesn't allow a whar input (as far as i can see).

    #include 
    #include 
    #include 
    
    int main()
    {
        //The input strings
        std::string str1 = "Hello";
        std::string str2 = "hello World";
    
        //Define the regular expression using case-insensitivity
        std::regex regx(str1, std::regex_constants::icase);
    
        //Only search at the beginning 
        std::regex_constants::match_flag_type fl = std::regex_constants::match_continuous;
    
        //display some output
        std::cout << std::boolalpha << std::regex_search(str2.begin(), str2.end(), regx, fl) << std::endl;
    
        return 0;
    }
    

提交回复
热议问题