LYF-LIUDAO
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();查找最后一次出现的位置

分类:

技术点:

相关文章:

  • 2021-11-13
  • 2021-07-07
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-04-07
猜你喜欢
  • 2022-12-23
  • 2021-08-23
  • 2021-12-12
  • 2022-01-21
  • 2021-12-14
  • 2021-10-15
相关资源
相似解决方案