分割字符串操作,我写了如下代码...
#include <iostream>
#include <string>
#include <vector>
#include <boost/algorithm/string/classification.hpp>
#include <boost/algorithm/string/split.hpp>using namespace std;int main()
{
char s; string str = "0.0724       0.0000       0.0000,\"W \",\"\"";
vector<string> rs;
boost::split( rs, str, boost::is_any_of( " ," ), boost::token_compress_on );
for( vector<string>::iterator it = rs.begin(); it != rs.end(); ++ it )
cout << *it << endl; cin >> s;
return 0;
}
待分割的字符串就是
0.0724       0.0000       0.0000,"W ",""
数字之间可能是空格也可能是TAB隔开的
"W "应该被割成W,或者"W"都行
""应该解析成空字符串现在我写的boost::is_any_of( " ," )在"W "这里解错了...因为把这段字符解成了W和"两部分,实际上引号是不要的
如果换成boost::is_any_of( " ,\"" )呢,最后那个没有解析成空字符串...也不对不知道我说清楚没...
纠结了...
求教...

解决方案 »

  1.   

    你可以先把string放进数组中,然后一个个的匹配ASCII码自己解析
      

  2.   

    一个字符串分隔至于用boost么?
    strtok,istringstream轻松搞定。
      

  3.   

    不知你这个字符串有没有代表性
    先用strtok分一下,再判断双引号数据的合法性也不是全靠程序来校验的,来源必须规范
      

  4.   

    数据倒是挺规范的.
    就是类似于CSV的一行行的字符串
    只是一行里,前面是空格分割,到后面变成都好分割了
    然后字符串是用双引号引起来的
    空字符串就是""我就卡在空字符串上了...
    因为我把"当分隔符拿boost一分割,空字符串就割没了