gpt4 book ai didi

c++ - 如何在C++中提取符号前的数字

转载 作者:行者123 更新时间:2023-12-05 09:04:29 29 4
gpt4 key购买 nike

我试过谷歌搜索,但就是找不到我的答案。

我有一个字符串“0-9”。我想提取破折号前后的数字并将其存储在变量中

e.g
string A = "0-9";
output:
min = 0
max = 9

我设法在破折号之后得到了数字,但之前没有。

string str = "0-9";
string max = str.substr(str.find("-") + 1);
cout << max;

它也应该适用于“10-20”。

最佳答案

https://www.cplusplus.com/reference/string/string/substr/

string substr (size_t pos = 0, size_t len = npos) const;

string str = "0-9";
string min = str.substr(0, str.find("-"));
cout << min;

关于c++ - 如何在C++中提取符号前的数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68564683/

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