如何检查单个字符是否至少等于给定字符集中的一个? [英] How do I check if a single char is equal to at least one of given set of char?

查看:77
本文介绍了如何检查单个字符是否至少等于给定字符集中的一个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对不起,标题。随时将其编辑为更清晰的内容。

Sorry for the title. Feel free to edit it to anything more clear.

我有一个字符串,必须检查该字符串的第一个字符至少等于另一个给定字符字符,例如B,Z和K(在我的情况下,我需要检查10个字符,它们不能归类为范围)。

I have a string and I have to check that the first char of this string is equal to at least one between other given char, for example B, Z and K (in my case I've about 10 char to check and they are not classifiable as a range).

if (string[0] == 'Z' || string[0] == 'K' || string[0] == 'B') {
   /* do something... */
}

有没有更简单的方法?

推荐答案

一种可能的方法是以字符串形式列出目标字符并使用 strchr

One possible approach would be to list your target chars in a string and use strchr

const char* matches = "ZKB...";
if (strchr(matches, string[0]) != NULL) {
    /* do something */
}

这篇关于如何检查单个字符是否至少等于给定字符集中的一个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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