c++ string 编写下面程序
张三|3456123, 湖南
李四,4564234| 湖北
王小二, 4433253|北京
...
以 “|” “,”为分隔符,同时又要过滤空格,把每行分成相应的字段。
通过运用string里面的内容编写。
[解决办法]
- C/C++ code
#include <iostream> // 数据流输入/输出#include <string> // 字符串类#include <algorithm> // STL 通用算法using namespace std;int main(){ string str = "张三 | 3456123 , 湖南"; size_t pos = 0, posend = 0;// pos = str.find_first_not_of("| ," , 0); // 搜索第一个,其实可以合并到下面 while里// posend = str.find_first_of("| ," , 0);// cout << str.substr(pos, posend) << endl; while ((pos = str.find_first_not_of("| ," , posend)) != string::npos) { posend = str.find_first_of("| ," , pos); cout << str.substr(pos, posend - pos) << endl; // substr 第二个参数不是pos,是个长度 } return 0;}/* find_first_of() 查找第一个与value中的某值相等的字符 * find_first_not_of() 查找第一个与value中的所有值都不相等的字符 * find_last_of() 查找最后一个与value中的某值相等的字符 * find_last_not_of() 查找最后一个与value中的所有值都不相等的字符 */