可以使用字符串的 'substr' 函数来提取字符串中的子串。'substr' 函数接受两个参数,第一个参数是子串的起始位置,第二个参数是子串的长度。由于要提取冒号后面的内容,所以起始位置应该是冒号的下一个位置,长度可以设为字符串的长度减去冒号的位置再减去1。

下面是一个示例代码:

#include <iostream>
#include <string>

int main() {
    std::string str = "wdy:32";
    unsigned int temp;

    std::string tempStr = str.substr(str.find(":") + 1);
    temp = std::stoul(tempStr);

    std::cout << "temp: " << temp << std::endl;

    return 0;
}

在上面的代码中,使用 'find' 函数找到冒号的位置,然后再用 'substr' 函数提取冒号后面的内容。最后使用 'std::stoul' 函数将提取到的字符串转换为 'unsigned int' 类型的值,并赋给 'temp' 变量。

运行上述代码,输出结果为:

temp: 32
C++ 字符串提取:从 'wdy:32' 中提取数字

原文地址: https://www.cveoy.top/t/topic/qinI 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录