#include <iostream>
#include <algorithm>
#include <functional>
using namespace std; class Teststring
{
public: void Toupper(string& str); //转大写
void Tolower(string& str); //转小写
void TrimRight(string& str); //去除后面的空格
void TrimLeft(string& str); //去除前面的空格
void DeleteChar(string& str,char ch); //去掉字符串里某个字符,注意,是都去掉
void TestReplace(); //替换字符串里的某一段字符 例如 abcdef 将ab替换成cd
bool StartWith(const string& str,const string& strStart); //字符串以某段开头
bool EndWith(const string& str,const string& strEnd); //字符串以某段结尾
void TestReverse(); //翻转字符串
}; void Teststring::Toupper(string& str)
{
transform(str.begin(),str.end(),str.begin(),::toupper);
} void Teststring::Tolower(string& str)
{
transform(str.begin(),str.end(),str.begin(),::tolower);
} void Teststring::TrimLeft(string& str)
{
str.erase(,str.find_first_not_of(' '));
}
void Teststring::TrimRight(string& str)
{
str.erase(str.find_last_not_of(' ')+);
} void Teststring::DeleteChar(string& str,char ch)
{
str.erase(remove_if(str.begin(),str.end(),bind2nd(equal_to<char>(), ch)),str.end());
} void Teststring::TestReplace()
{
string str = "abcdef";
string strSub = "ab";
str.replace(str.find(strSub),strSub.size(),"cd");
cout<<str.c_str()<<endl;
} bool Teststring::StartWith(const string& str,const string& strStart)
{
if(str.empty() || strStart.empty())
{
return false;
} return str.compare(,strStart.size(),strStart)==?true:false;
} bool Teststring::EndWith(const string& str,const string& strEnd)
{
if(str.empty() || strEnd.empty())
{
return false;
}
return str.compare(str.size()-strEnd.size(),strEnd.size(),strEnd)==?true:false;
} //翻转汉字的过程中,会出现乱码,因为该方法是按char进行翻转的
void Teststring::TestReverse()
{
string str1 = "张东升";
std::reverse(str1.begin(),str1.end());
string str2 = "";
str2.assign(str1.rbegin(),str1.rend()); }
转自:
https://blog.****.net/kwsy2008/article/details/11533329