gpt4 book ai didi

c++ - 如何从一个字符串中提取多个子字符串?

转载 作者:行者123 更新时间:2023-12-02 09:58:25 27 4
gpt4 key购买 nike

我原来的字符串看起来像这个<Information name="Identify" id="IdentifyButton" type="button"/>从此字符串中,如何提取3个子字符串string name_part = Identifystring id_part="IdentifyButton"type_part="button"

最佳答案

假设您不想使用第三方XML解析器,则可以对每个名称使用std::stringfind():

int main()
{
std::string s("<Information name = \"Identify\" id = \"IdentifyButton\" type = \"button\" / >");
std::string names[] = { "name = \"" , "id = \"" , "type = \"" };
std::string::size_type posStart(0), posEnd(0);
for (auto& n : names)
{
posStart = s.find(n, posEnd) + n.length();
posEnd = s.find("\"", posStart);
std::string part = s.substr(posStart, posEnd - posStart);
std::cout << part << std::endl;
posEnd++;
}
}
根据您的容忍度添加错误检查:)

关于c++ - 如何从一个字符串中提取多个子字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64017823/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com