C++ 检查字符串是否包含数字的函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9642292/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 13:07:00  来源:igfitidea点击:

Function to check if string contains a number

c++classfunctionnumbers

提问by sinθ

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?

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

   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;
    }

回答by Benjamin Lindley

Change all your ||to &&.

将您的所有更改||&&.

Better yet:

更好的是:

return std::find_if(s.begin(), s.end(), ::isdigit) != s.end();        

Or, if you have it:

或者,如果您拥有它:

return std::any_of(s.begin(), s.end(), ::isdigit);

回答by Peter Wood

C++11:

C++11:

#include <algorithm>
#include <cctype>
#include <string>
#include <iostream>

bool has_any_digits(const std::string& s)
{
    return std::any_of(s.begin(), s.end(), ::isdigit);
}

int main()
{
    std::string query("H311o, W0r1d!");

    std::cout << query << ": has digits: "
              << std::boolalpha
              << has_any_digits(query)
              << std::endl;
    return 1;
}

Output:

输出:

H311o, W0r1d!: has digits: true

H311o, W0r1d!: has digits: true

回答by Remy Lebeau

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

它总是返回,false因为您的逻辑是倒退的。您正在使用||带有== npos检查的运算符。如果字符串中缺少任何一个特定数字,则== npos评估为true||满足,因此您返回false. 您需要使用!= npos检查,然后返回,true如果任何检查评估为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;
}

Or:

或者:

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
    );
}

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

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

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

回答by prelic

How to test if a string contains any digits in C++

如何测试字符串是否包含 C++ 中的任何数字

This should do it!

这个应该可以!

if (std::string::npos != s.find_first_of("0123456789"))
{
  std::cout << "digit(s)found!" << std::endl;
}

回答by Paolo Brandoli

You are using ||(or operator) to check several conditions in an if statement. The or operator returns true (satisfies the condition) if one of the expression is true.

您正在使用||(或运算符)检查 if 语句中的多个条件。如果表达式之一为真,或运算符返回真(满足条件)。

The or operator evaluates first the expression on its left: if that is true then it doesn't evaluate the expression on its right and returns true. If the expression on the left is false then the expression on the right is evaluated and the result of it is returned as result of || operator

or 运算符首先计算其左侧的表达式:如果为真,则不计算其右侧的表达式并返回 true。如果左边的表达式为假,则计算右边的表达式并将其结果作为 || 的结果返回 操作员

This is what happen in your function:

这就是您的函数中发生的情况:

  • does c contains '0' ? if no (because std::string::npos in find() means not found) then return false
  • does c contains '1' ? if no then return false
  • ...
  • c 包含 '0' 吗?如果没有(因为 find() 中的 std::string::npos 表示未找到)则返回 false
  • c 包含 '1' 吗?如果没有,则返回 false
  • ...

So, replace the or operators with && (and operator).

因此,将 or 运算符替换为 && (和运算符)。