C++获取两个分隔符之间的字符串并替换它

C++ get string between two delimiters and replace it

本文关键字:字符 字符串 串并 替换 之间 分隔符 获取 两个 C++      更新时间:2023-10-16

我想用依赖于分隔符之间的子字符串的东西来替换字符串中的子字符串。小例子:

我得到了字符串

The result is __--__3__--__.

和一个功能

int square(int x): { return x*x };

现在我只想输出不带分隔符的结果字符串,所以:

The result is 9.

我已经尝试了几种算法,但没有一种有效。最受尊敬的

我迄今为止最好的尝试:

const std::string emptyString = "";
std::string ExtractString(std::string source, std::string start, std::string end)
{
std::size_t startIndex = source.find(start);
// If the starting delimiter is not found on the string
// stop the process, you're done!
//
if (startIndex == std::string::npos)
{
return emptyString;
}
// Adding the length of the delimiter to our starting index
// this will move us to the beginning of our sub-string.
//
startIndex += start.length();
// Looking for the end delimiter
//
std::string::size_type endIndex = source.find(end, startIndex);
// Returning the substring between the start index and
// the end index. If the endindex is invalid then the
// returned value is empty string.
return source.substr(startIndex, endIndex - startIndex);
}
int square(int x): { return x*x };
int main() {
std::string str = "The result is __--__3__--__.";
std::string foundNum = ExtractString(str, "__--__", "__--__");
int foundNumInt = atoi(foundNum.c_str());
int result = square(foundNumInt);
std::string toReplace = "__--__";
toReplace.append(foundNumInt);
toReplace.append("__--__");
str.replace(str.begin(), str.end(), toReplace, result);
}

问题是:如何获取给定的第一个字符串(The result is __--__<number>__--__.>,从中获取数字,在该数字上预生成一个函数,然后以类似于The result is <number squared>的字符串结束。

这里有一种获取第一个字符串的方法,找到数字。然后我把这个数字平方,但你可以把它插入你自己想要的函数中

std::string s = "The result is __--__3__--__.";
std::regex r( "[0-9]+");
std::smatch m;
//
std::sregex_iterator iter(s.begin(), s.end(), r);
std::sregex_iterator end;
std::string value;
//
int index = 0;
while (iter != end)
{
for (unsigned i = 0; i < iter->size(); ++i)
{
value = (*iter)[i];
}
++iter;
index++;
}
int num = stoi(value);
int answer = num*num;
s = s.substr(0, s.find('_'));
s = s + " " + std::to_string(answer);
std::cout << s << std::endl;

您尝试过std::string::find吗?

const std::string example_data = "The result is __--__3__--__.";
static const char text_to_find[] = "__--__";
const std::string::size_type start_position = example_data.find(text_to_find);
if (start_position != std::string::npos)
{
const std::string::size_type replacement_start_position = start_position + sizeof(text_to_find) - 1;
if (replacement_start_position < example_data.length())
{
// Perform replacement
}
}

"sizeof(text_to_find(-1"返回文本的长度,不计算终止的nul字符。

要跳过该数字,您可以执行以下操作:

const std::string after_number_position = example_data.find(replacement_start_position, "_");

replacement_start_positionafter_number_position之间的子字符串将包含您的数字。可以使用各种函数将子字符串转换为数字。

有关将数字转换为文本的信息,请参见std::ostringstream

编辑1:
更正了replacement_start_position的声明

您必须需要以下函数(对于c++17,速度要快得多(:

auto replace_all
(std::string str, std::string_view from, std::string_view to) noexcept -> decltype(str) {
unsigned start_pos{ 0 };
while ((start_pos = str.find(from, start_pos)) != std::string::npos) {
str.replace(start_pos, from.length(), to);
start_pos += to.length();
}
return str;
}
auto remove_all
(std::string str, std::string_view from) noexcept -> decltype(str) {
return replace_all(str, from, "");
}

对于更高版本:

std::string replace_all
(std::string str, std::string from, std::string to) noexcept {
unsigned start_pos{ 0 };
while ((start_pos = str.find(from, start_pos)) != std::string::npos) {
str.replace(start_pos, from.length(), to);
start_pos += to.length();
}
return str;
}
std::string remove_all
(std::string str, std::string from) noexcept {
return replace_all(str, from, "");
}

我测试过:

int main() {
std::string str = "__+__hello__+__";
std::cout << remove_all(str, "__+__");
std::cin.get();
return 0;
}

我的输出是:

hello