snipt

Ctrl+h for KB shortcuts
Notice: Snipt is moving away from free accounts on May 1st, 2014. Read more about the transition here.
#28393

C++

Split a comma-separated string into a whitespace-trimmed sequence of non-empty tokens

1
2
3
4
5
6
std::string s; // Source data
std::vector<std::string> tokens;
boost::algorithm::split(tokens, s, boost::algorithm::is_any_of(","));
std::for_each(tokens.begin(), tokens.end(),
              boost::bind(boost::algorithm::trim<std::string>, _1, std::locale()));
tokens.erase(std::remove(tokens.begin(), tokens.end(), ""), tokens.end());
https://snipt.net/embed/f50cf584128efbca9194cd83643608a0/
https://snipt.net/raw/f50cf584128efbca9194cd83643608a0/
f50cf584128efbca9194cd83643608a0
cpp
C++
7
2014-04-24T04:16:22
True
False
/api/public/snipt/28393/
split-a-comma-separated-string-into-a-whitespace-trimmed-sequence-of-non-empty-tokens
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a> <a href="#L-5">5</a> <a href="#L-6">6</a></pre></div></td><td class="code"><div class="highlight"><pre><span id="L-1"><a name="L-1"></a><span class="n">std</span><span class="o">::</span><span class="n">string</span> <span class="n">s</span><span class="p">;</span> <span class="c1">// Source data</span> </span><span id="L-2"><a name="L-2"></a><span class="n">std</span><span class="o">::</span><span class="n">vector</span><span class="o">&lt;</span><span class="n">std</span><span class="o">::</span><span class="n">string</span><span class="o">&gt;</span> <span class="n">tokens</span><span class="p">;</span> </span><span id="L-3"><a name="L-3"></a><span class="n">boost</span><span class="o">::</span><span class="n">algorithm</span><span class="o">::</span><span class="n">split</span><span class="p">(</span><span class="n">tokens</span><span class="p">,</span> <span class="n">s</span><span class="p">,</span> <span class="n">boost</span><span class="o">::</span><span class="n">algorithm</span><span class="o">::</span><span class="n">is_any_of</span><span class="p">(</span><span class="s">&quot;,&quot;</span><span class="p">));</span> </span><span id="L-4"><a name="L-4"></a><span class="n">std</span><span class="o">::</span><span class="n">for_each</span><span class="p">(</span><span class="n">tokens</span><span class="p">.</span><span class="n">begin</span><span class="p">(),</span> <span class="n">tokens</span><span class="p">.</span><span class="n">end</span><span class="p">(),</span> </span><span id="L-5"><a name="L-5"></a> <span class="n">boost</span><span class="o">::</span><span class="n">bind</span><span class="p">(</span><span class="n">boost</span><span class="o">::</span><span class="n">algorithm</span><span class="o">::</span><span class="n">trim</span><span class="o">&lt;</span><span class="n">std</span><span class="o">::</span><span class="n">string</span><span class="o">&gt;</span><span class="p">,</span> <span class="n">_1</span><span class="p">,</span> <span class="n">std</span><span class="o">::</span><span class="n">locale</span><span class="p">()));</span> </span><span id="L-6"><a name="L-6"></a><span class="n">tokens</span><span class="p">.</span><span class="n">erase</span><span class="p">(</span><span class="n">std</span><span class="o">::</span><span class="n">remove</span><span class="p">(</span><span class="n">tokens</span><span class="p">.</span><span class="n">begin</span><span class="p">(),</span> <span class="n">tokens</span><span class="p">.</span><span class="n">end</span><span class="p">(),</span> <span class="s">&quot;&quot;</span><span class="p">),</span> <span class="n">tokens</span><span class="p">.</span><span class="n">end</span><span class="p">());</span> </span></pre></div> </td></tr></table>
csv, parse, string