HappyDay1234567890
您可以使用std::string::find()函数查找字符串分隔符的位置,然后使用std::string::substr()去拿个记号。例子:std::string s = "scott>=tiger";std::string delimiter = ">=";std::string token = s.substr(0, s.find(delimiter)); // token is "scott"这个find(const string& str, size_t pos = 0)函数返回str在字符串中,或npos如果找不到字符串。这个substr(size_t pos = 0, size_t n = npos)函数返回对象的子字符串,从位置开始。pos和长度npos.如果有多个分隔符,则在提取一个令牌后,可以删除它(包括分隔符)以继续后续提取(如果要保留原始字符串,只需使用s = s.substr(pos + delimiter.length());):s.erase(0, s.find(delimiter) + delimiter.length());这样,您就可以轻松地循环获得每个令牌。完整示例std::string s = "scott>=tiger>=mushroom";std::string delimiter = ">=";size_t pos = 0;std:
:string token;while ((pos = s.find(delimiter)) != std::string::npos) {
token = s.substr(0, pos);
std::cout <
s.erase(0, pos + delimiter.length());}std::cout <
tiger
mushroom