gpt4 book ai didi

c++ - 你如何在 C++ 中搜索 std::string 的子字符串?

转载 作者:可可西里 更新时间:2023-11-01 18:02:07 25 4
gpt4 key购买 nike

我正在尝试用 C++ 解析一个简单的字符串。我知道该字符串包含一些带冒号的文本,紧接着是一个空格,然后是一个数字。我只想提取字符串的数字部分。我不能只标记空格(使用 sstream 和 <<),因为冒号前面的文本可能有也可能没有空格。

一些示例字符串可能是:

Total disk space: 9852465

Free disk space: 6243863

Sectors: 4095

我想使用标准库,但如果您有其他解决方案,您也可以将其发布,因为有相同问题的其他人可能希望看到不同的解决方案。

最佳答案

std::string strInput = "Total disk space: 9852465";
std::string strNumber = "0";
size_t iIndex = strInput.rfind(": ");
if(iIndex != std::string::npos && strInput.length() >= 2)
{
strNumber = strInput.substr(iIndex + 2, strInput.length() - iIndex - 2)
}

关于c++ - 你如何在 C++ 中搜索 std::string 的子字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/346858/

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