获取不知道长度c+的子字符串+; 在阅读C++电子书的时候,我想到了下一个问题:
假设我们有一个任意长度的字符串获取不知道长度c+的子字符串+; 在阅读C++电子书的时候,我想到了下一个问题:,c++,string,C++,String,假设我们有一个任意长度的字符串str。此字符串有两个单词start和end。其思想是在start和end之间获得字符串的子字符串。是否有任何方法可以获得类似于string::substr的结果,但不知道start和end之间的距离?当然,我的意思是,使用copy和迭代器或任何循环都很容易做到这一点。但是可能有一个方法在string中实现?我在C++引用中找不到一个。< P>如果你有一个包含数据的字符串,你想在一个开始和结束的文本位之间得到所有的字符串,你可以使用下面的。 std::string
str
。此字符串有两个单词start
和end
。其思想是在start
和end
之间获得字符串的子字符串。是否有任何方法可以获得类似于string::substr
的结果,但不知道start
和end
之间的距离?当然,我的意思是,使用copy
和迭代器或任何循环都很容易做到这一点。但是可能有一个方法在string
中实现?我在C++引用中找不到一个。< P>如果你有一个包含数据的字符串,你想在一个开始和结束的文本位之间得到所有的字符串,你可以使用下面的。
std::string test = "this is a string we are searching through to get a substring";
std::string startText = "string";
std::string endText = "to";
size_t startPos, endPos;
startPos = test.find(startText, 0);
if (startPos == std::string::npos)
std::cout << "No Starting Text";
endPos = test.find(endText, startPos + 1);
if (endPos == std::string::npos)
std::cout << "No Ending Text";
std::string subString = test.substr(startPos + startText.size() + 1, endPos - startPos + startText.size());
std::cout << subString;
std::string test=“这是我们正在搜索以获取子字符串的字符串”;
std::string startText=“string”;
std::string endText=“to”;
大小\u t开始位置,结束位置;
startPos=test.find(startText,0);
if(startPos==std::string::npos)
std::cout最佳解决方案是找到第一个出现的os“开始”和最后一个出现的“结束”:
Live示例:。您可以使用std::string类的成员函数或标准算法以不同的方式完成任务
下面介绍这两种方法
第一个使用类std::string
#include <iostream>
#include <string>
#include <cstring>
int main()
{
std::string s( "startmiddleend" );
const char *start = "start";
const char *end = "end";
std::string t;
std::cout << s << std::endl;
auto pos = s.find( start );
if ( pos != std::string::npos )
{
pos += std::strlen( start );
auto n = s.find( end, pos );
if ( n == std::string::npos ) n = s.size();
t = s.substr( pos, n - pos );
}
std::cout << t << std::endl;
}
两个程序具有相同的输出
程序输出为
startmiddleend
middle
问题的答案是:否提取此类文本没有简单的方法
我碰巧有一个我之前准备的函数,它可以很好地实现这一点。我会提供它,以防它对某人有用:
#include <string>
#include <iostream>
using size_type = std::string::size_type;
size_type extract_delimited_text(const std::string& in
, const std::string& d1, const std::string& d2
, std::string& out, size_type pos = 0)
{
auto end = pos;
if((pos = in.find(d1, pos)) != std::string::npos)
{
if((end = in.find(d2, (pos = pos + d1.size()))) != std::string::npos)
{
out = in.substr(pos, end - pos);
return end + d2.size();
}
}
return std::string::npos;
}
int main()
{
std::string d1 = "${";
std::string d2 = "}";
std::string s = "find stuff ${to extract} and stuff and ${some more} stuff";
std::string sub;
std::string::size_type pos = 0;
// keep extracting all matches
while((pos = extract_delimited_text(s, d1, d2, sub, pos)) != std::string::npos)
std::cout << "sub: " << sub << '\n';
}
你必须自己写一些东西,或者一个正则表达式可以这样做。std::string::find,然后substr?“这个字符串有两个单词…”所以你实际上想解析一个空格分隔的字符串,或者什么?找到“start”,找到“end”,然后取由你找到的两个位置分隔的substr。。正如deviantfan所说的^^@deviantfan是的,我们会找到'start',但是写'str.substr(str.find(“start”,0),?)'不会产生效果。我想删掉这些词之间的子串以外的所有内容。一般情况下,“结束”可以在“开始”之前
#include <iostream>
#include <string>
#include <cstring>
int main()
{
std::string s( "startmiddleend" );
const char *start = "start";
const char *end = "end";
std::string t;
std::cout << s << std::endl;
auto pos = s.find( start );
if ( pos != std::string::npos )
{
pos += std::strlen( start );
auto n = s.find( end, pos );
if ( n == std::string::npos ) n = s.size();
t = s.substr( pos, n - pos );
}
std::cout << t << std::endl;
}
#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
#include <iterator>
int main()
{
std::string s( "startmiddleend" );
const char *start = "start";
const char *end = "end";
std::string t;
std::cout << s << std::endl;
size_t n = std::strlen( start );
auto first = std::search( s.begin(), s.end(), start, start + n );
if ( first != s.end() )
{
std::advance( first, n );
t.assign( first, std::search( first, s.end(), end, end + std::strlen( end ) ) );
}
std::cout << t << std::endl;
}
startmiddleend
middle
#include <string>
#include <iostream>
using size_type = std::string::size_type;
size_type extract_delimited_text(const std::string& in
, const std::string& d1, const std::string& d2
, std::string& out, size_type pos = 0)
{
auto end = pos;
if((pos = in.find(d1, pos)) != std::string::npos)
{
if((end = in.find(d2, (pos = pos + d1.size()))) != std::string::npos)
{
out = in.substr(pos, end - pos);
return end + d2.size();
}
}
return std::string::npos;
}
int main()
{
std::string d1 = "${";
std::string d2 = "}";
std::string s = "find stuff ${to extract} and stuff and ${some more} stuff";
std::string sub;
std::string::size_type pos = 0;
// keep extracting all matches
while((pos = extract_delimited_text(s, d1, d2, sub, pos)) != std::string::npos)
std::cout << "sub: " << sub << '\n';
}
sub: to extract
sub: some more