拆解字符串

tech2023-01-23  104

#include #include #include

using namespace std;

void test1() { string str = “www.beijing.com.cn”; vector v; int start = 0; int pos = -1; while (true) { pos = str.find(".", start); if (pos==-1) { //将cn截取出来 string tempStr = str.substr(start, str.size() - start); v.push_back(tempStr); break; }

string tempStr = str.substr(start, pos - start); v.push_back(tempStr); start = pos + 1; } for (vector<string>::iterator it = v.begin(); it != v.end(); it++) { cout << *it << endl; }

}

int main() { test1(); }

最新回复(0)