0
如何在*字符前提取C++中所有字符的子字符串。举例来说,如果我有一个字符串提取子字符串的C++正则表达式
ASDG::DS"G*0asd}}345sdgfsdfg
我将如何提取部分
ASDG::DS"G
如何在*字符前提取C++中所有字符的子字符串。举例来说,如果我有一个字符串提取子字符串的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;
}