Arduino - 字符函数

Arduino - 字符函数 首页 / Arduino入门教程 / Arduino - 字符函数

所有数据都以字符形式输入计算机,其中包括字母,数字和各种特殊符号。

请记住,EOF通常具有值–1,并且某些硬件体系结构不允许将负值存储在char变量中,因此,字符处理函数将字符操作为整数。

S.No.Prototype & 描述
1

int isdigit(int c)

如果c是数字,则返回1,否则返回0。

2

int isalpha(int c)

如果c是字母,则返回1,否则返回0。

3

int isalnum(int c)

如果c是数字或字母,则返回1,否则返回0。

4

int isxdigit(int c)

如果c是十六进制数字字符,则返回1,否则返回0。

5

int islower(int c)

如果c是小写字母,则返回1,否则返回0。

6

int isupper(int c)

如果c是大写字母,则返回1;否则,返回0。否则为0。

7

int isspace(int c)

如果c是空格字符-换行符('\n'),则返回1

(''),换页('\f'),回车('\r'),水平制表符('\t')或垂直制表符('\v')-否则为0。

8

int iscntrl(int c)

如果c是控制字符,如换行符('\n'),换页符('\f'),回车符('\r'),水平制表符('\t'),垂直制表符('\v'),警报('\a')或退格键('\b')-否则为0。

9

int ispunct(int c)

如果c是空格,数字或字母以外的其他打印字符,则返回1,否则返回0。

10

int isprint(int c)

如果c是包含空格('')的打印字符,则返回1,否则返回0。

11

int isgraph(int c)

如果c是除空格('')以外的其他打印字符,则返回1,否则返回0。

下面的示例演示了 isdigit,isalpha,isalnum 和 isxdigit 函数的用法,函数 isdigit 确定其自变量是否为数字(0–9),函数 isalpha 确定其自变量是大写字母(A-Z)还是小写字母(a–z),函数 isalnum 确定其参数是大写,小写字母还是数字,函数 isxdigit 确定其自变量是否为十六进制数字(AF,AF,0-9)。

void setup () {
   Serial.begin (9600);
   Serial.print ("According to isdigit:\r");
   Serial.print (isdigit( '8' ) ? "8 is a": "8 is not a");
   Serial.print (" digit\r" );
   Serial.print (isdigit( '8' ) ?"# is a": "# is not a") ;
   Serial.print (" digit\r");
   Serial.print ("\rAccording to isalpha:\r" );
   Serial.print (isalpha('A' ) ?"A is a": "A is not a");
   Serial.print (" letter\r");
   Serial.print (isalpha('A' ) ?"b is a": "b is not a");
   Serial.print (" letter\r");
   Serial.print (isalpha('A') ?"& is a": "& is not a");
   Serial.print (" letter\r");
   Serial.print (isalpha( 'A' ) ?"4 is a":"4 is not a");
   Serial.print (" letter\r");
   Serial.print ("\rAccording to isalnum:\r");
   Serial.print (isalnum( 'A' ) ?"A is a" : "A is not a" );

   Serial.print (" digit or a letter\r" );
   Serial.print (isalnum( '8' ) ?"8 is a" : "8 is not a" ) ;
   Serial.print (" digit or a letter\r");
   Serial.print (isalnum( '#' ) ?"# is a" : "# is not a" );
   Serial.print (" digit or a letter\r");
   Serial.print ("\rAccording to isxdigit:\r");
   Serial.print (isxdigit( 'F' ) ?"F is a" : "F is not a" );
   Serial.print (" hexadecimal digit\r" );
   Serial.print (isxdigit( 'J' ) ?"J is a" : "J is not a" ) ;
   Serial.print (" hexadecimal digit\r" );
   Serial.print (isxdigit( '7' ) ?"7 is a" : "7 is not a" ) ;

   Serial.print (" hexadecimal digit\r" );
   Serial.print (isxdigit( '$' ) ? "$ is a" : "$ is not a" );
   Serial.print (" hexadecimal digit\r" );
   Serial.print (isxdigit( 'f' ) ? “f is a" : "f is not a");
   
}

void loop () {

}

执行上面代码输出如下:

According to isdigit:
8 is a digit
# is not a digit
According to isalpha:
A is a letter
b is a letter
& is not a letter
4 is not a letter
According to isalnum:
A is a digit or a letter

8 is a digit or a letter
# is not a digit or a letter
According to isxdigit:
F is a hexadecimal digit
J is not a hexadecimal digit
7 is a hexadecimal digit

$is not a hexadecimal digit
f is a hexadecimal digit

示例二

下面的示例演示 islower 和 isupper 函数的用法,函数 islower 确定其参数是否为小写字母(a–z),函数 isupper 确定其参数是否为大写字母(A–Z)。

int thisChar=0xA0;

void setup () {
   Serial.begin (9600);
   Serial.print ("According to islower:\r") ;
   Serial.print (islower( 'p' ) ? "p is a" : "p is not a" );
   Serial.print ( " lowercase letter\r" );
   Serial.print ( islower( 'P') ? "P is a" : "P is not a") ;
   Serial.print ("lowercase letter\r");
   Serial.print (islower( '5' ) ? "5 is a" : "5 is not a" );
   Serial.print ( " lowercase letter\r" );
   Serial.print ( islower( '!' )? "! is a" : "! is not a") ;
   Serial.print ("lowercase letter\r");

   Serial.print ("\rAccording to isupper:\r") ;
   Serial.print (isupper ( 'D' ) ? "D is a" : "D is not an" );
   Serial.print ( " uppercase letter\r" );
   Serial.print ( isupper ( 'd' )? "d is a" : "d is not an") ;
   Serial.print ( " uppercase letter\r" );
   Serial.print (isupper ( '8' ) ? "8 is a" : "8 is not an" );
   Serial.print ( " uppercase letter\r" );
   Serial.print ( islower( '$' )? "$is a" : "$is not an") ;
   Serial.print ("uppercase letter\r ");
}

void setup () {

}

执行上面代码输出如下:

According to islower:
p is a lowercase letter
P is not a lowercase letter
5 is not a lowercase letter
! is not a lowercase letter

According to isupper:
D is an uppercase letter
d is not an uppercase letter
8 is not an uppercase letter
$is not an uppercase letter

示例三

以下示例演示了 isspace,iscntrl,ispunct,isprint 和 isgraph 函数的用法。

  • isspace  -  确定其参数是否为空格字符,如空格(''),换页('\f'),换行符('\n' ),回车符('\r'),水平制表符('\t')或垂直制表符('\v')。

  • iscntrl    -  确定其参数是否为控制字符,如水平制表符('\t'),垂直制表符('\v'),换页符('\f '),警报('\a'),退格键('\b'),回车('\r')或换行符('\n')。

  • ispunct  -  确定其参数是否为空格,数字或字母以外的其他印刷字符,如$,#,(,),[,],{,} ,;,:或%。

  • isprint    -  确定其参数是否为可以在屏幕上显示的字符(包括空格字符)。

  • isgraph  -  测试与isprint相同的字符,但不包括空格字符。

void setup () {
   Serial.begin (9600);
   Serial.print ( " According to isspace:\rNewline ") ;
   Serial.print (isspace( '\n' )? " is a" : " is not a" );
   Serial.print ( " whitespace character\rHorizontal tab") ;
   Serial.print (isspace( '\t' )? " is a" : " is not a" );
   Serial.print ( " whitespace character\n") ;
   Serial.print (isspace('%')? " % is a" : " % is not a" );
   
   Serial.print ( "\rAccording to iscntrl:\rNewline") ;
   Serial.print ( iscntrl( '\n' )?"is a" : " is not a" ) ;
   Serial.print (" control character\r");
   Serial.print (iscntrl( '$' ) ? " $is a" : " $is not a" );
   Serial.print (" control character\r");
   Serial.print ("\rAccording to ispunct:\r");
   Serial.print (ispunct(';' ) ?"; is a" : "; is not a" ) ;
   Serial.print (" punctuation character\r");
   Serial.print (ispunct('Y' ) ?"Y is a" : "Y is not a" ) ;
   Serial.print ("punctuation character\r");
   Serial.print (ispunct('#' ) ?"# is a" : "# is not a" ) ;
   Serial.print ("punctuation character\r");

   Serial.print ( "\r According to isprint:\r");
   Serial.print (isprint('$' ) ?"$is a" : "$is not a" );
   Serial.print (" printing character\rAlert ");
   Serial.print (isprint('\a' ) ?" is a" : " is not a" );
   Serial.print (" printing character\rSpace ");
   Serial.print (isprint(' ' ) ?" is a" : " is not a" );
   Serial.print (" printing character\r");
   
   Serial.print ("\r According to isgraph:\r");
   Serial.print (isgraph ('Q' ) ?"Q is a" : "Q is not a" );
   Serial.print ("printing character other than a space\rSpace ");
   Serial.print (isgraph (' ') ?" is a" : " is not a" );
   Serial.print ("printing character other than a space ");
}

void loop () {

}

执行上面代码输出如下:

According to isspace:
Newline is a whitespace character
Horizontal tab is a whitespace character
% is not a whitespace character
According to iscntrl:
Newline is a control character
$is not a control character
According to ispunct:
; is a punctuation character
Y is not a punctuation character
# is a punctuation character
According to isprint:
$is a printing character
Alert is not a printing character
Space is a printing character
According to isgraph:
Q is a printing character other than a space
Space is not a printing character other than a space

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

许式伟的架构课 -〔许式伟〕

玩转webpack -〔程柳锋〕

NLP实战高手课 -〔王然〕

跟月影学可视化 -〔月影〕

Go 语言项目开发实战 -〔孔令飞〕

HarmonyOS快速入门与实战 -〔QCon+案例研习社〕

零基础实战机器学习 -〔黄佳〕

深入剖析Java新特性 -〔范学雷〕

Python实战 · 从0到1搭建直播视频平台 -〔Barry〕

好记忆不如烂笔头。留下您的足迹吧 :)