首页 > 解决方案 > 如何将字符串的第一个完整数字删除为整数 C++

问题描述

如何在 C++ 中将字符串的第一个完整数字删除为整数

例如字符串“thdfwrhwh456dfhdfh764”

只需要提取第一个数字 456 作为整数。

谢谢

标签: c++string

解决方案


首先找到第一个数字:

std::size_t pos = str.find_first_of(“0123456789”);

然后检查是否找到了一个数字:

if (pos != std::string::npos)

然后提取字符串的尾部:

std::string tail = str.substr(pos);

然后提取值:

int value = std::stoi(tail);

推荐阅读