简单介绍下如何对字符串中的子串进行增删改查,并通过下面实例的伪代码进行简单展示
字符串子串的查找
string strfind = "test for find [user] login system";
string key {"[user]"};
auto pos = strfind.find(key);
if(pos != string::npos){ //判断一下是否存在子串
}
字符串子串的删除
string strfind = "test for find [user] login system";
string key{"user"};
auto pos = strfind.find(key);
if(pos != string::npos){
for (int i = pos; i <= strfind.size(); i++)
{
cout << strfind[i + key.size()];
strfind[i] = strfind[i + key.size()];
}
}
字符串子串的替换
string strfind = "test for find [user] login system";
string key {"[user]"};
auto pos = strfind.find(key);
if(pos != string::npos){
auto rep = strfind.replace(pos, key.size(), "root");
}
字符串子串增加
//末尾增加字串
string strfind = "test for find [user] login system";
string substr{"user"};
strfind += substr;