多个字符的比较,我怎么能更聪明地编写它? [英] Multiple character comparisons, how can I write it smarter?
问题描述
我正在解析一个文本文件.解析时,我想跳过某些字符(空格,换行符,逗号,句点).在PHP中,可以使用 in_array(char,array)
检查数组中变量的存在,但是鉴于我们正在使用指针,因此情况显然有所不同.
I am parsing a text-file. While parsing, I want to skip certain characters (space, line-break, comma, period). In PHP, one may check the existence of a variable in an array with in_array(char, array)
, but things are obviously different given we are working with pointers.
我目前正在这样写(请问奇怪的格式)
I am currently writing it like this (excuse the weird formatting)
if (c == ' ' ||
c == '\n' ||
c == '.' ||
c == ',') {
continue;
}
但是感觉有点傻.有没有更聪明/更紧凑的方式来执行这样的多次比较?
But it feels a bit dumb. Is there a smarter/more compact way to perform multiple comparisons like this?
推荐答案
另一种选择是使用 strchr
检查给定字符是否在给定字符串中:
Another choice would be to use strchr
to check if a given character is in a given string:
if (strchr(" \n.,", c)
continue;
这篇关于多个字符的比较,我怎么能更聪明地编写它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!