使用C++将一个字符串替换为另一个字符串
Replace a string to another string using C++
问题是我不知道输入字符串的长度。
我的函数只能在输入字符串为 "yyyy" 时进行替换。我想到的解决方案是,首先,我们将尝试将输入字符串转换回 "yyyy" 并使用我的函数来完成工作。
这是我的函数:
void findAndReplaceAll(std::string & data, std::string toSearch, std::string replaceStr)
{
// Get the first occurrence
size_t pos = data.find(toSearch);
// Repeat till end is reached
while( pos != std::string::npos)
{
// Replace this occurrence of Sub String
data.replace(pos, toSearch.size(), replaceStr);
// Get the next occurrence from the current position
pos = data.find(toSearch, pos + replaceStr.size());
}
}
我的主要功能
std::string format = "yyyyyyyyyydddd";
findAndReplaceAll(format, "yyyy", "%Y");
findAndReplaceAll(format, "dd", "%d");
我的预期输出应该是:
%Y%d
使用正则表达式。
示例:
#include <iostream>
#include <string>
#include <regex>
int main(){
std::string text = "yyyyyy";
std::string sentence = "This is a yyyyyyyyyyyy.";
std::cout << "Text: " << text << std::endl;
std::cout << "Sentence: " << sentence << std::endl;
// Regex
std::regex y_re("y+"); // this is the regex that matches y yyy or more yyyy
// replacing
std::string r1 = std::regex_replace(text, y_re, "%y"); // using lowercase
std::string r2 = std::regex_replace(sentence, y_re, "%Y"); // using upercase
// showing result
std::cout << "Text replace: " << r1 << std::endl;
std::cout << "Sentence replace: " << r2 << std::endl;
return 0;
}
输出:
Text: yyyyyy
Sentence: This is a yyyyyyyyyyyy.
Text replace: %y
Sentence replace: This is a %Y.
如果你想让它变得更好,你可以使用:
// Regex
std::regex y_re("[yY]+");
这将匹配任意数量的 'Y' 的任意大小写混合。
使用该正则表达式的示例输出:
Sentence: This is a yYyyyYYYYyyy.
Sentence replace: This is a %Y.
这只是您可以使用正则表达式执行的操作的一个简单示例,我建议您查看主题本身,在 SO 和其他站点中有很多关于她的信息。
额外:
如果你想在替换之前匹配以交替替换你可以这样做:
// Regex
std::string text = "yyaaaa";
std::cout << "Text: " << text << std::endl;
std::regex y_re("y+"); // this is the regex that matches y yyy or more yyyy
std::string output = "";
std::smatch ymatches;
if (std::regex_search(text, ymatches, y_re)) {
if (ymatches[0].length() == 2 ) {
output = std::regex_replace(text, y_re, "%y");
} else {
output = std::regex_replace(text, y_re, "%Y");
}
}
问题是我不知道输入字符串的长度。 我的函数只能在输入字符串为 "yyyy" 时进行替换。我想到的解决方案是,首先,我们将尝试将输入字符串转换回 "yyyy" 并使用我的函数来完成工作。
这是我的函数:
void findAndReplaceAll(std::string & data, std::string toSearch, std::string replaceStr)
{
// Get the first occurrence
size_t pos = data.find(toSearch);
// Repeat till end is reached
while( pos != std::string::npos)
{
// Replace this occurrence of Sub String
data.replace(pos, toSearch.size(), replaceStr);
// Get the next occurrence from the current position
pos = data.find(toSearch, pos + replaceStr.size());
}
}
我的主要功能
std::string format = "yyyyyyyyyydddd";
findAndReplaceAll(format, "yyyy", "%Y");
findAndReplaceAll(format, "dd", "%d");
我的预期输出应该是:
%Y%d
使用正则表达式。
示例:
#include <iostream>
#include <string>
#include <regex>
int main(){
std::string text = "yyyyyy";
std::string sentence = "This is a yyyyyyyyyyyy.";
std::cout << "Text: " << text << std::endl;
std::cout << "Sentence: " << sentence << std::endl;
// Regex
std::regex y_re("y+"); // this is the regex that matches y yyy or more yyyy
// replacing
std::string r1 = std::regex_replace(text, y_re, "%y"); // using lowercase
std::string r2 = std::regex_replace(sentence, y_re, "%Y"); // using upercase
// showing result
std::cout << "Text replace: " << r1 << std::endl;
std::cout << "Sentence replace: " << r2 << std::endl;
return 0;
}
输出:
Text: yyyyyy
Sentence: This is a yyyyyyyyyyyy.
Text replace: %y
Sentence replace: This is a %Y.
如果你想让它变得更好,你可以使用:
// Regex
std::regex y_re("[yY]+");
这将匹配任意数量的 'Y' 的任意大小写混合。 使用该正则表达式的示例输出:
Sentence: This is a yYyyyYYYYyyy.
Sentence replace: This is a %Y.
这只是您可以使用正则表达式执行的操作的一个简单示例,我建议您查看主题本身,在 SO 和其他站点中有很多关于她的信息。
额外: 如果你想在替换之前匹配以交替替换你可以这样做:
// Regex
std::string text = "yyaaaa";
std::cout << "Text: " << text << std::endl;
std::regex y_re("y+"); // this is the regex that matches y yyy or more yyyy
std::string output = "";
std::smatch ymatches;
if (std::regex_search(text, ymatches, y_re)) {
if (ymatches[0].length() == 2 ) {
output = std::regex_replace(text, y_re, "%y");
} else {
output = std::regex_replace(text, y_re, "%Y");
}
}