string类查找功能

int main ()
{
    std::string StringTest = "hello/the/world";
    int index = StringTest.find('/');

    //取得第一个'/'之前的字符串
    std::string StringTest2 = StringTest.substr(0,index);
    std::cout<< "StringTest2 = "<< StringTest2.c_str() <<std::endl;
    //取得第一个'/'之后的字符串
    std::string StringTest3 = StringTest.substr(index + 1,StringTest.length() );
    std::cout << "StringTest3 = "<< StringTest3.c_str() <<std::endl;

    //注:以此类推,获得world也可以采取同样的方法
    return 0;
}

string类自带查找功能,上述代码很方便实用。

补充:

rfind();查找子字符串或字符最后一次出现的位置

find_first_of();查找首次出现的位置

find_last_of();查找最后一次出现的位置

原文地址:https://www.cnblogs.com/LYF-LIUDAO/p/7000004.html