[英]C++ regex to extract substring
如何在c ++中提取*字符之前的所有字符的子字符串。 例如,如果我有一个字符串
ASDG::DS"G*0asd}}345sdgfsdfg
我将如何提取该部分
ASDG::DS"G
您当然不需要正则表达式。 只需使用std::string::find('*')
和std::string::substr
:
#include <string>
int main()
{
// raw strings require C++-11
std::string s1 = R"(ASDG::DS"G*0asd}}345sdgfsdfg)";
std::string s2 = s1.substr(0, s1.find('*'));
}
我认为您的文本没有多个*
因为find
返回的第一个*
#include <iostream>
#include <string>
using namespace std;
#define SELECT_END_CHAR "*"
int main(){
string text = "ASDG::DS\"G*0asd}}345sdgfsdfg";
unsigned end_index = text.find(SELECT_END_CHAR);
string result = text.substr (0,end_index);
cout << result << endl;
system("pause");
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.