且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

C ++字符串基于分隔符的修改和提取

更新时间:2023-02-26 09:28:32

对于C风格字符串,您可以使用 strtok()来做到这一点。您也可以使用 sscanf()

With C-style strings you can use strtok() to do this. You could also use sscanf()

但是,因为你在处理C ++,你可能想坚持使用内置的std :: string函数。因此,你可以使用find()。 Find有一个表单,它接受第二个参数,它是开始搜索的偏移量。所以你可以找到(':')找到第一个实例,然后使用find(':',firstIndex + 1)来查找下一个实例,其中firstIndex是第一次调用find()时返回的值。

But since you're dealing with C++, you probably want to stick with built in std::string functions. As such you can use find(). Find has a form which takes a second argument which is the offset to start searching. So you can do find( ':' ) to find the first instance, and then use find( ':', firstIndex+1 ) to find the next instances, where firstIndex is the value returned by the first call to find().