Find if a string contains a character in C++ (boost allowed)

后端 未结 7 807
遇见更好的自我
遇见更好的自我 2020-12-31 05:58

Suppose I have a string and I want to find whether a specific character (like \'|\') is present or not, what is the best and fastest technique to do so? I know string find i

7条回答
  •  轮回少年
    2020-12-31 06:33

    Another way is to use the strchr function on the corresponding c_str string:

    if(strchr(str.c_str(), '|'))
    {
        \\found
    }
    

    Not sure how it compares to std find in terms of speed though...

    The position of the found character is

    size_t pos = strchr(str.c_str(),'|') - str.c_str();
    

提交回复
热议问题