how to check string start in C++

后端 未结 12 1999
Happy的楠姐
Happy的楠姐 2020-11-29 03:31

Is there any way in C++ to check whether a string starts with a certain string (smaller than the original) ? Just like we can do in Java

bigString.startswi         


        
12条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-29 04:35

    With C++20 you can use std::basic_string::starts_with (or std::basic_string_view::starts_with):

    #include 
    
    std::string_view bigString_v("Winter is gone"); // std::string_view avoids the copy in substr below.
    std::string_view smallString_v("Winter");
    if (bigString_v.starts_with(smallString_v))
    {
        std::cout << "Westeros" << bigString_v.substr(smallString_v.size());
    }
    

提交回复
热议问题