Is There A Built-In Way to Split Strings In C++?

前端 未结 10 2209
心在旅途
心在旅途 2020-12-08 10:56

well is there? by string i mean std::string

10条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-08 11:38

    STL strings

    You can use string iterators to do your dirty work.

    std::string str = "hello world";
    
    std::string::const_iterator pos = std::find(string.begin(), string.end(), ' '); // Split at ' '.
    
    std::string left(str.begin(), pos);
    std::string right(pos + 1, str.end());
    
    // Echoes "hello|world".
    std::cout << left << "|" << right << std::endl;
    

提交回复
热议问题