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

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

问题描述

抱歉标题.随意将其编辑为更清晰的内容.

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天全站免登陆