如何检查一个字符串是否全部由小写字母和数字组成?

4

有没有一种方法可以检查这些情况?还是说我需要解析字符串中的每个字母,并检查它是否为小写字母(letter)和数字/字母?

6个回答

5
你可以使用 islower()isalnum() 来检查每个字符是否符合这些条件。没有字符串级别的函数可以实现,因此你需要自己编写代码。

好的,我已经编辑过来了以反映出这一点。我之前不知道那个函数。 - Oleksi
我在这里有点困惑,你用“好观点...”的评论回复了自己的答案。 - t0mm13b
@t0mm13b 在我发表评论之前,有一个评论指出了isalnum()函数。 我想它已经被删除了:P - Oleksi
1
不好意思,您仍需等待发布问题后的X分钟才能接受答案,但还是谢谢。 - unwise guy
2
@Oleksi:我完全没问题你的代码被接受了——虽然这个函数确实存在,但我并不确定我会推荐使用它。我相信大多数人会发现在循环中或传递给std::find的类似islowerisalnum的函数更容易理解。 - Jerry Coffin
显示剩余3条评论

3
假设“C”语言环境是可接受的(或者可以替换为不同的字符集),使用 find_first_not_of() 。
#include <string>

bool testString(const std::string& str)
{
      std::string criteria("abcdefghijklmnopqrstuvwxyz0123456789");
      return (std::string::npos == str.find_first_not_of(criteria);
}

假设“C”语言环境(这可能对OP无关紧要)。 - aschepler
1
@bgporter 很抱歉两年后才回复,但这种方法效率如何?我正在开发一个化学方程式平衡器,这似乎是一个理想的预检查方法,而不是对每个字符执行多次检查。 - Andrue
我猜问题是“相对于什么而言高效?”我的第一选择是假设标准库通常足够快(且相对无错误),与手写代码相比。 - bgporter
@Andrue,这不是很高效,因为std :: find_first_not_of需要一个内部循环来检查元素是否不在集合中,而不像isdigitisalnumislower可以根据语言环境仅使用几个比较。 - phuclv

3
不是非常有名,但是一个locale实际上确实有函数来确定整个字符串的特征。具体而言,locale的ctype facet具有scan_is和scan_not,用于扫描符合指定掩码(字母、数字、字母数字、小写、大写、标点符号、空格、十六进制数字等)的第一个字符,或者不符合它的第一个字符,分别返回指向字符串中第一个不符合要求的项的指针,否则类似于std::find_if,返回您传递的“end”以表示失败。这是一个快速示例:
#include <locale>
#include <iostream>
#include <iomanip>

int main() {

    std::string inputs[] = { 
        "alllower",
        "1234",
        "lower132",
        "including a space"
    };

    // We'll use the "classic" (C) locale, but this works with any
    std::locale loc(std::locale::classic());

    // A mask specifying the characters to search for:          
    std::ctype_base::mask m = std::ctype_base::lower | std::ctype_base::digit;

    for (int i=0; i<4; i++) {
        char const *pos;
        char const *b = &*inputs[i].begin();
        char const *e = &*inputs[i].end();

        std::cout << "Input: " << std::setw(20) << inputs[i] << ":\t";

        // finally, call the actual function:
        if ((pos=std::use_facet<std::ctype<char> >(loc).scan_not(m, b, e)) == e)
            std::cout << "All characters match mask\n";
        else
            std::cout << "First non-matching character = \"" << *pos << "\"\n";
    }
    return 0;
}

我猜大多数人会更喜欢使用std :: find_if,因为使用它几乎相同,但是可以很容易地推广到更多情况。尽管这具有更窄的适用性,但实际上并不比用户更容易(尽管我认为如果您正在扫描大量文本,则可能至少会快一点)。


0
你可以使用 tolower 和 strcmp 来比较原始字符串和小写字符串。并且对每个字符单独处理数字。
(或者)像下面这样对每个字符都进行处理。
#include <algorithm>

static inline bool is_not_alphanum_lower(char c)
{
    return (!isalnum(c) || !islower(c));
}

bool string_is_valid(const std::string &str)
{
    return find_if(str.begin(), str.end(), is_not_alphanum_lower) == str.end();
}

我使用了一些信息来自于: 判断一个字符串是否只包含字母数字字符(或空格)


0

只需使用{{link1:std::all_of}}

bool lowerAlnum = std::all_of(str.cbegin(), str.cend(), [](const char c){
    return isdigit(c) || islower(c);
});

如果您不关心语言环境(即输入为纯7位ASCII),则可将该条件优化为

[](const char c){ return ('0' <= c && c <= '9') || ('a' <= c && c <= 'z'); }

-1
如果您的字符串包含ASCII编码文本,并且您喜欢编写自己的函数(就像我一样),那么您可以使用以下内容:
bool is_lower_alphanumeric(const string& txt)
{
  for(char c : txt)
  {
    if (!((c >= '0' and c <= '9') or (c >= 'a' and c <= 'z'))) return false;
  }
  return true;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接