Tag Archives: boost

Permalink to single post

C++での文字列区切り

CSV文字列を区切る方法 by boost

探していたので一応メモ

改行とかのエスケープ処理もこなしてくれるようで、完璧すぎる。。

string s = "Field 1,\"putting quotes around fields, allows commas\",Field 3";
tokenizer<escaped_list_separator<char> > tok(s);
for(tokenizer<escaped_list_separator<char> >::iterator beg=tok.begin(); beg!=tok.end();++beg)
    cout << *beg << "\n";

詳細は→Escaped List Separator

簡単に文字区切りをしたい場合は

algorithm::split(parts, str, algorithm::is_any_of(",|"));

で可能。