{"id":2438,"date":"2015-11-13T13:00:38","date_gmt":"2015-11-13T12:00:38","guid":{"rendered":"http:\/\/roboblog.fatal-fury.de\/?p=2438"},"modified":"2022-09-21T07:54:05","modified_gmt":"2022-09-21T06:54:05","slug":"stdstring-trimmed","status":"publish","type":"post","link":"http:\/\/roboblog.fatal-fury.de\/?p=2438","title":{"rendered":"std::string trimmed std::string_view"},"content":{"rendered":"<p>There is no trim() trimmed() member function in std::string. A free function is better.<\/p>\n<p>Here is one nice solution which has a linear running time and not quadratic like using erase(). (Stupid programmer out there)<br \/>\nFrom:<br \/>\n<a href=\"http:\/\/stackoverflow.com\/questions\/216823\/whats-the-best-way-to-trim-stdstring\">http:\/\/stackoverflow.com\/questions\/216823\/whats-the-best-way-to-trim-stdstring<\/a><\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nstd::string trimmed(const std::string &amp;s) {\r\n   auto wsfront=std::find_if_not(s.begin(),s.end(), &#x5B;](int c){return std::isspace(c);});\r\n   auto wsback=std::find_if_not(s.rbegin(),s.rend(),&#x5B;](int c){return std::isspace(c);}).base();\r\n   return (wsback&lt;=wsfront ? std::string() : std::string(wsfront,wsback));\r\n}\r\n<\/pre>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nstd::string_view trimmed(std::string_view s) {\r\n    auto wsfront = std::find_if_not(s.begin(),s.end(), &#x5B;](int c){return std::isspace(c);});\r\n    \/\/ base() returns the forward iterator from reverse iterator returned by rbegin()\r\n    auto wsback = std::find_if_not(s.rbegin(),s.rend(),&#x5B;](int c){return std::isspace(c);}).base();\r\n    return wsback &lt;= wsfront ? std::string_view() : std::string_view(wsfront, wsback-wsfront); \/\/  its a lower than there. F wordpress\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>There is no trim() trimmed() member function in std::string. A free function is better. Here is one nice solution which has a linear running time and not quadratic like using erase(). (Stupid programmer out there) From: http:\/\/stackoverflow.com\/questions\/216823\/whats-the-best-way-to-trim-stdstring std::string trimmed(const std::string &amp;s) { auto wsfront=std::find_if_not(s.begin(),s.end(), &#x5B;](int c){return std::isspace(c);}); auto wsback=std::find_if_not(s.rbegin(),s.rend(),&#x5B;](int c){return std::isspace(c);}).base(); return (wsback&lt;=wsfront ? std::string() [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[17],"class_list":["post-2438","post","type-post","status-publish","format-standard","hentry","category-allgemein","tag-cpp"],"_links":{"self":[{"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=\/wp\/v2\/posts\/2438","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2438"}],"version-history":[{"count":4,"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=\/wp\/v2\/posts\/2438\/revisions"}],"predecessor-version":[{"id":5065,"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=\/wp\/v2\/posts\/2438\/revisions\/5065"}],"wp:attachment":[{"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2438"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2438"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2438"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}