c++ - 如何将字符串的第一个完整数字删除为整数 C++
问题描述
如何在 C++ 中将字符串的第一个完整数字删除为整数
例如字符串“thdfwrhwh456dfhdfh764”
只需要提取第一个数字 456 作为整数。
谢谢
解决方案
首先找到第一个数字:
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);
推荐阅读
- list - 循环宏:如何同时将局部变量收集到多个列表中?
- html - 使用 ul 的 Z-index 和 displax flex
- python - 未打开项目的 Revit Python Shell
- python - 缺失值/pandas、性别列
- flutter - 将 QueryDocumentSnapshot 转换为模型类型会导致异常
- postgresql - Postgres:当 1 行失败时,允许其他行的批量插入成功
- python - 使用图像帧坐标确定相机坐标以进行平面姿态估计
- typescript - Jest 不会在 NextJS 应用程序中解析 SVG 图片
- java - Spring Data Couchbase:Count() 和 findAll() 不起作用,但 findBy() 起作用
- java - Springfox Swagger 仅忽略嵌套对象的 @XmlElement