0

如何在 C++ 中提取 * 字符之前所有字符的子字符串。例如,如果我有一个字符串

ASDG::DS"G*0asd}}345sdgfsdfg

我将如何提取该部分

ASDG::DS"G
4

2 回答 2

4

你当然不需要正则表达式。只需使用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('*'));
}
于 2013-04-11T06:25:09.067 回答
0

我认为您的文本没有多个*,因为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;
}
于 2013-04-11T06:32:15.703 回答