检查字符串是否包含数字的函数 [英] Function to check if string contains a number

查看:133
本文介绍了检查字符串是否包含数字的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个项目在c + +(我刚刚开始学习),不明白为什么这个功能不工作。我试图写一个具有变量first_name的Person类,并使用函数set_first_name设置名称。 Set_first_name需要调用一个函数(下面的一个)来检查名称中是否有任何数字。该函数总是返回false,我想知道为什么?此外,这是检查数字的最好方法,还是有更好的方法?

I'm working on a project in c++ (which I just started learning) and can't understand why this function is not working. I'm attempting to write a "Person" class with a variable first_name, and use a function set_first_name to set the name. Set_first_name needs to call a function(the one below) to check if the name has any numbers in it. The function always returns false, and I'm wondering why? Also, is this the best way to check for numbers, or is there a better way?

   bool Person::contains_number(std::string c){ // checks if a string contains a number
        if (c.find('0') == std::string::npos || c.find('1') == std::string::npos || c.find('2') == std::string::npos || c.find('3') == std::string::npos
        || c.find('4') == std::string::npos || c.find('5') == std::string::npos || c.find('6') == std::string::npos || c.find('7') == std::string::npos
        || c.find('8') == std::string::npos || c.find('9') == std::string::npos){// checks if it contains number

        return false;
        }
        return true;
    }


推荐答案

$ c> false ,因为你的逻辑是向后的。您正在使用 || 运算符与 == npos 检查。如果字符串中缺少任何一个特定数字, == npos 计算为 true || ,因此您返回 false 。您需要使用!= npos 检查,然后返回 true 如果任何检查计算结果为 true

It always returns false because your logic is backwards. You are using the || operator with == npos checks. If any one particular digit is missing from the string, == npos evaluates to true and || is satisfied, so you return false. You need to using != npos checks and then return true if any check evaluates to true:

bool Person::contains_number(const std::string &c)
{
    if (c.find('0') != std::string::npos ||
        c.find('1') != std::string::npos ||
        c.find('2') != std::string::npos ||
        c.find('3') != std::string::npos ||
        c.find('4') != std::string::npos ||
        c.find('5') != std::string::npos ||
        c.find('6') != std::string::npos ||
        c.find('7') != std::string::npos ||
        c.find('8') != std::string::npos ||
        c.find('9') != std::string::npos)
    {
        return true;
    }

    return false;
}

或:

bool Person::contains_number(const std::string &c)
{
    return (
        c.find('0') != std::string::npos ||
        c.find('1') != std::string::npos ||
        c.find('2') != std::string::npos ||
        c.find('3') != std::string::npos ||
        c.find('4') != std::string::npos ||
        c.find('5') != std::string::npos ||
        c.find('6') != std::string::npos ||
        c.find('7') != std::string::npos ||
        c.find('8') != std::string::npos ||
        c.find('9') != std::string::npos
    );
}

一个更简单的解决方案是使用 find_first_of / code>而不是 find()

A simplier solution is to use find_first_of() instead of find():

bool Person::contains_number(const std::string &c)
{
    return (c.find_first_of("0123456789") != std::string::npos);
}    

这篇关于检查字符串是否包含数字的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆