Convert String to lowercase
#include <algorithm>
#include <string>
string data = "Abc";
std::transform(data.begin(), data.end(), data.begin(), ::tolower);
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 :-(
#include <algorithm>
#include <string>
std::string string1 = "Mx string";
std::replace(string1.begin(), string1.end(), 'x', 'y'); // replace all 'x' with 'y' (needs the <algorithm> library)
//#include <algorithm>
//#include <string>
//***************************************************************************************
//***************************************************************************************
//********** REPLACE AN OCCURANCE OF A STRING WITHIN A STRING CASE INSENSITIVE **********
//***************************************************************************************
//***************************************************************************************
//ReplaceFrom will be compared without case sensitivity to ConvertString
string ReplaceAllCaseInsensitive(string ConvertString, const string ReplaceFrom, const string ReplaceTo)
{
if (ReplaceFrom.length() <= 0) //Protect against endless loop
return(ConvertString);
//Get lowercase versions of string to be converted to use for compare, and ensure the ReplaceFrom string is also lowercase
string ConvertStringLowercase = ConvertString;
std::transform(ConvertStringLowercase.begin(), ConvertStringLowercase.end(), ConvertStringLowercase.begin(), ::tolower);
string ReplaceFromLowercase = ReplaceFrom;
std::transform(ReplaceFromLowercase.begin(), ReplaceFromLowercase.end(), ReplaceFromLowercase.begin(), ::tolower);
//Do the replace
size_t StartPos = 0;
if ((StartPos = ConvertStringLowercase.find(ReplaceFromLowercase, StartPos)) != std::string::npos)
{
ConvertString.replace(StartPos, ReplaceFrom.length(), ReplaceTo);
//StartPos += ReplaceTo.length(); // Handles case where 'ReplaceTo' is a substring of 'ReplaceFrom'
}
return(ConvertString);
}
Replace String In A String
//#include <algorithm>
//#include <string>
//************************************************************************
//************************************************************************
//********** REPLACE ALL OCCURANCES OF A STRING WITHIN A STRING **********
//************************************************************************
//************************************************************************
string ReplaceAll(string ConvertString, const string ReplaceFrom, const string ReplaceTo)
{
if (ReplaceFrom.length() <= 0) //Protect against endless loop
return(ConvertString);
//Do the replace
size_t StartPos = 0;
while((StartPos = ConvertString.find(ReplaceFrom, StartPos)) != std::string::npos)
{
ConvertString.replace(StartPos, ReplaceFrom.length(), ReplaceTo);
StartPos += ReplaceTo.length(); // Handles case where 'ReplaceTo' is a substring of 'ReplaceFrom'
}
return(ConvertString);
}
Remove Character In A String
#include <algorithm>
#include <string>
std::string string1 = "Mx string";
string1.erase(std::remove(string1.begin(), string1.end(), 'x'), string1.end()); // remove all 'x' characters (needs the <algorithm> library)
Replace the first and last occurances of characters in a string
string my_string = "{abc}";
size_t found_character;
//Remove the first and last braces (will be added by json handler later)
found_character = my_string.find_first_of("{");
if (found_character != string::npos)
my_string[found_character]=' ';
found_character = my_string.find_last_of("}");
if (found_character != string::npos)
my_string[found_character]=' ';
Insert String Into String
my_string.insert(start_pos, some_other_string);
USEFUL?
We benefit hugely from resources on the web so we decided we should try and give back some of our knowledge and resources to the community by opening up many of our company’s internal notes and libraries through mini sites like this. We hope you find the site helpful.
Please feel free to comment if you can add help to this page or point out issues and solutions you have found, but please note that we do not provide support on this site. If you need help with a problem please use one of the many online forums.