简体   繁体   English

检查C中的特殊字符

[英]Check for Special Characters in C

I got some help on how to see if a "string" in C contains a specific character. 我对如何查看C中的“字符串”是否包含特定字符有一些帮助。 In short: 简而言之:

if(*s=='x') { //Where x is some character
    //Do something
}

Now, as far as I can see, this works for letters in the English alphabet (az, AZ). 现在,据我所知,这适用于英文字母(az,AZ)中的字母。

However, how can I check if the current character equals a special character (such as æ , ø , or å )? 但是,如何检查当前字符是否等于特殊字符(例如æøå )?

In most cases you should be using a function for this: strchr for looking for a single byte, or strstr for looking for more than a single byte. 在大多数情况下,您应该为此使用一个函数: strchr用于查找单个字节,或者strstr用于查找多个字节。

Also, in general C does not know about characters, it only knows about bytes. 同样,通常C不知道字符,只知道字节。 A special character may be a single byte - æ in iso8859-1 encoding is \\xe6 , for example - or it may be more than one byte: the same character in utf-8 encoding is the 2-byte sequence \\xc3\\xa6 . 特殊字符可以是单个字节-例如,iso8859-1编码中的æ是\\xe6或可以大于一个字节:utf-8编码中的同一字符是2字节序列\\xc3\\xa6

To search a utf-8 encoded string for æ you could use 要搜索utf-8编码的字符串以查找æ,可以使用

strstr(s, "\xc3\xa6")

Just compare to the ASCII code of the character: 只需将其与字符的ASCII码进行比较即可:

if(*s==10) { //Where 10 is the ASCII code of the special character
    //Do something
}

You can find your ASCII code here: http://www.asciitable.com/ 您可以在这里找到您的ASCII代码: http : //www.asciitable.com/

Compare the character to the actual value (as long as the value is between 0 and 255). 将字符与实际值进行比较(只要该值介于0到255之间)。

See the æ wiki page for particular values. 见æ 维基特定值的页面。 So 所以

if (*s == 0xe6) 如果(* s == 0xe6)

for lower case 小写

if (*s == 0xc6) 如果(* s == 0xc6)

for upper case 大写

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM