{"id":2418,"date":"2016-01-15T11:19:55","date_gmt":"2016-01-15T11:19:55","guid":{"rendered":"https:\/\/raspberry-projects.com\/pi\/?p=2418"},"modified":"2016-01-15T13:35:46","modified_gmt":"2016-01-15T13:35:46","slug":"string-functions","status":"publish","type":"post","link":"https:\/\/raspberry-projects.com\/pi\/programming-in-c\/boost-c-libraries\/string-functions","title":{"rendered":"String functions"},"content":{"rendered":"<p>\n&nbsp;\n<\/p>\n<pre>\r\n<code>\r\n#include &lt;boost\/algorithm\/string.hpp&gt;\r\n#include &lt;boost\/tokenizer.hpp&gt;\t\t\t\t\/\/May be needed\r\nusing namespace std;\r\nusing namespace boost;\r\nusing namespace boost::algorithm;\r\n<\/code><\/pre>\n<p>\n<a href=\"http:\/\/www.boost.org\/doc\/libs\/1_56_0\/doc\/html\/string_algo.html\">http:\/\/www.boost.org\/doc\/libs\/1_56_0\/doc\/html\/string_algo.html<\/a>\n<\/p>\n<p>\n<a href=\"http:\/\/theboostcpplibraries.com\/boost.stringalgorithms\">http:\/\/theboostcpplibraries.com\/boost.stringalgorithms<\/a>\n<\/p>\n<h4>\nAlter&nbsp;supplied string vs Create a&nbsp;new string from the output<br \/>\n<\/h4>\n<p>\nMany of the string functions some in function_name() and function_name_copy() versions,&nbsp;where the _copy() version returns the output and the non&nbsp;_copy() version instead alters the supplied string.\n<\/p>\n<h4>\nTest A String<br \/>\n<\/h4>\n<h5>\nDoes string contain<br \/>\n<\/h5>\n<pre>\r\n<code>\r\n\tif (contains(my_string, &quot;hello&quot;)\r\n\t\t\/\/do something\r\n<\/code><\/pre>\n<h4>\nGet Portions Of A String<br \/>\n<\/h4>\n<h5>\nTrim<br \/>\n<\/h5>\n<pre>\r\n<code>\r\n\ttrim(temp_string);\r\n<\/code><\/pre>\n<p>\nGet string up to first\n<\/p>\n<h5>\nRead each line of a sting<br \/>\n<\/h5>\n<pre>\r\n<code>\r\n\tstring text(&quot;apple\\n\\norange\\npair&quot;);\r\n\r\n\ttypedef boost::tokenizer&lt;boost::char_separator&lt;char&gt; &gt; line_tokenizer;\r\n\tline_tokenizer tok(text, boost::char_separator&lt;char&gt;(&quot;\\n\\r&quot;));\r\n\r\n\tfor (line_tokenizer::const_iterator i = tok.begin(), end = tok.end(); i != end ; ++i)\t\t\/\/Get each line of the string, skipping past blank lines\r\n\t\tstd::cout &lt;&lt; &quot;A:&quot; &lt;&lt; *i &lt;&lt; std::endl;\r\n<\/code><\/pre>\n<h5>\nSplit up a string by character<br \/>\n<\/h5>\n<pre>\r\n<code>\r\n\tstring my_text = &quot;07\/3\/2011&quot;;\r\n\tvector&lt;string&gt; split_line_elements;\r\n\tboost::split(split_line_elements, my_text, boost::is_any_of(&quot;\/&quot;));\r\n\tif (split_line_elements.size() &gt; 0)\r\n\t\t\/\/do something\r\n<\/code><\/pre>\n<h4>\nRemove Bits Of A String<br \/>\n<\/h4>\n<h5>\nRemove all occurances of a character<br \/>\n<\/h5>\n<pre>\r\n<code>\r\n\tboost::erase_all(my_string, &quot;a&quot;);\r\n<\/code><\/pre>\n<p>\n&nbsp;\n<\/p>\n<p>\n&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; #include &lt;boost\/algorithm\/string.hpp&gt; #include &lt;boost\/tokenizer.hpp&gt; \/\/May be needed using namespace std; using namespace boost; using namespace boost::algorithm; http:\/\/www.boost.org\/doc\/libs\/1_56_0\/doc\/html\/string_algo.html http:\/\/theboostcpplibraries.com\/boost.stringalgorithms Alter&nbsp;supplied string vs Create a&nbsp;new string from the output Many of the string functions some in function_name() and function_name_copy() versions,&nbsp;where the _copy() version returns the output and the non&nbsp;_copy() version instead alters the supplied string. Test [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[118],"tags":[],"class_list":["post-2418","post","type-post","status-publish","format-standard","hentry","category-boost-c-libraries"],"_links":{"self":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/2418","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/comments?post=2418"}],"version-history":[{"count":8,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/2418\/revisions"}],"predecessor-version":[{"id":2436,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/2418\/revisions\/2436"}],"wp:attachment":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/media?parent=2418"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/categories?post=2418"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/tags?post=2418"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}