我想用取决于定界符之间的子字符串的内容替换字符串中的子字符串。小例子:
我知道了字符串
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>
答案 0 :(得分:1)
这里是一种获取第一个字符串,查找数字的方法。然后,我只是对数字求平方,但您可以将其插入自己想要的功能中。
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;
答案 1 :(得分:0)
您尝试过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_position
和after_number_position
之间的子字符串将包含您的电话号码。您可以使用多种功能将子字符串转换为数字。
另请参阅std::ostringstream
,以将数字转换为文本。
编辑1:
更正了replacement_start_position
的声明。
答案 2 :(得分:0)
您必须需要以下功能(对于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