{"id":2067,"date":"2015-06-11T20:05:36","date_gmt":"2015-06-11T20:05:36","guid":{"rendered":"https:\/\/raspberry-projects.com\/pi\/?p=2067"},"modified":"2020-04-27T15:01:05","modified_gmt":"2020-04-27T15:01:05","slug":"using-strings-replace-and-insert","status":"publish","type":"post","link":"https:\/\/raspberry-projects.com\/pi\/programming-in-c\/strings\/string-class\/using-strings-replace-and-insert","title":{"rendered":"Using Strings-Replace and Insert"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">Convert String to lowercase<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;algorithm>\n#include &lt;string> \n\n\tstring data = \"Abc\"; \n\tstd::transform(data.begin(), data.end(), data.begin(), ::tolower);<\/code><\/pre>\n\n\n\n<p>Be aware that ::tolower() can only do a per-single-byte-character substitution, which is ill-fitting for many scripts, especially if using a multi-byte-encoding like UTF-8.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Replace Character In A String<\/h4>\n\n\n\n<p>Note this only works for characters, there is no simple function to replace sections of a string :-(<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;algorithm>\n#include &lt;string>\n\n\tstd::string string1 = \"Mx string\";\n\tstd::replace(string1.begin(), string1.end(), 'x', 'y');\t\/\/ replace all 'x' with 'y' (needs the &lt;algorithm> library)<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/#include &lt;algorithm>\n\/\/#include &lt;string>\n\/\/***************************************************************************************\n\/\/***************************************************************************************\n\/\/********** REPLACE AN OCCURANCE OF A STRING WITHIN A STRING CASE INSENSITIVE **********\n\/\/***************************************************************************************\n\/\/***************************************************************************************\n\/\/ReplaceFrom will be compared without case sensitivity to ConvertString \nstring ReplaceAllCaseInsensitive(string ConvertString, const string ReplaceFrom, const string ReplaceTo)\n{\n\tif (ReplaceFrom.length() &lt;= 0)\t\t\/\/Protect against endless loop\n\t\treturn(ConvertString);\n\n\t\/\/Get lowercase versions of string to be converted to use for compare, and ensure the ReplaceFrom string is also lowercase\n\tstring ConvertStringLowercase = ConvertString;\n\tstd::transform(ConvertStringLowercase.begin(), ConvertStringLowercase.end(), ConvertStringLowercase.begin(), ::tolower);\n\t\n\tstring ReplaceFromLowercase = ReplaceFrom;\n\tstd::transform(ReplaceFromLowercase.begin(), ReplaceFromLowercase.end(), ReplaceFromLowercase.begin(), ::tolower);\n\t\n\t\/\/Do the replace\n    size_t StartPos = 0;\n    if ((StartPos = ConvertStringLowercase.find(ReplaceFromLowercase, StartPos)) != std::string::npos)\n\t{\n        ConvertString.replace(StartPos, ReplaceFrom.length(), ReplaceTo);\n        \/\/StartPos += ReplaceTo.length();\t\t\t\/\/ Handles case where 'ReplaceTo' is a substring of 'ReplaceFrom'\n    }\n\treturn(ConvertString);\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Replace String In A String<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/#include &lt;algorithm>\n\/\/#include &lt;string>\n\/\/************************************************************************\n\/\/************************************************************************\n\/\/********** REPLACE ALL OCCURANCES OF A STRING WITHIN A STRING **********\n\/\/************************************************************************\n\/\/************************************************************************\nstring ReplaceAll(string ConvertString, const string ReplaceFrom, const string ReplaceTo)\n{\n\tif (ReplaceFrom.length() &lt;= 0)\t\t\/\/Protect against endless loop\n\t\treturn(ConvertString);\n\t\n\t\/\/Do the replace\n    size_t StartPos = 0;\n    while((StartPos = ConvertString.find(ReplaceFrom, StartPos)) != std::string::npos)\n\t{\n        ConvertString.replace(StartPos, ReplaceFrom.length(), ReplaceTo);\n        StartPos += ReplaceTo.length();\t\t\t\/\/ Handles case where 'ReplaceTo' is a substring of 'ReplaceFrom'\n    }\n\treturn(ConvertString);\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Remove Character In A String<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;algorithm>\n#include &lt;string>\n\n\tstd::string string1 = \"Mx string\";\n\tstring1.erase(std::remove(string1.begin(), string1.end(), 'x'), string1.end());\t\/\/ remove all 'x' characters (needs the &lt;algorithm> library)<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Replace the first and last occurances of characters in a string<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>\tstring my_string = \"{abc}\";\n\tsize_t found_character;\n\n\t\/\/Remove the first and last braces (will be added by json handler later)\n\tfound_character = my_string.find_first_of(\"{\");\n\tif (found_character != string::npos)\n\t\tmy_string&#91;found_character]=' ';\n\tfound_character = my_string.find_last_of(\"}\");\n\tif (found_character != string::npos)\n\t\tmy_string&#91;found_character]=' ';<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Insert String Into String<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>\tmy_string.insert(start_pos, some_other_string);<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Convert String to lowercase Be aware that ::tolower() can only do a per-single-byte-character substitution, which is ill-fitting for many scripts, especially if using a multi-byte-encoding like UTF-8. Replace Character In A String Note this only works for characters, there is no simple function to replace sections of a string :-( Replace String In A String [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[97],"tags":[],"class_list":["post-2067","post","type-post","status-publish","format-standard","hentry","category-string-class"],"_links":{"self":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/2067","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=2067"}],"version-history":[{"count":10,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/2067\/revisions"}],"predecessor-version":[{"id":3247,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/2067\/revisions\/3247"}],"wp:attachment":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/media?parent=2067"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/categories?post=2067"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/tags?post=2067"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}