Arduino - 字符函数

所有数据都以字符形式输入计算机,包括字母、数字和各种特殊符号。在本节中,我们将讨论 C++ 检查和处理单个字符的功能。

字符处理库包含几个函数,它们对字符数据执行有用的测试和处理。每个函数接收一个字符(表示为 int)或 EOF 作为参数。字符通常作为整数进行处理。

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

下表总结了字符处理库的函数。使用字符处理库中的函数时,请包含 <cctype> 标头。

S.No. 原型 &描述
1

int isdigit( int c )

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

2

int isalpha( int c )

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

3

int isalnum( int c )

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

4

int isxdigit( int c )

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

(有关二进制、八进制、十进制和十六进制数的详细说明,请参阅附录 D"数字系统"。)

5

int islower( int c )

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

6

int isupper( int c )

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

7

int isspace( int c )

如果 c 是空白字符(换行符 (' ')、空格

(' ')、换页符 ('\f')、回车符 (' ')、水平制表符 (' ') 或垂直制表符 ('\v')),则返回 1,否则返回 0。

8

int iscntrl( int c )

如果 c 是控制字符,例如换行符 (' ')、换页符 ('\f')、回车符 (' ')、水平制表符 (' ')、垂直制表符 ('\v')、警告符 ('\a') 或退格符 ('\b'),则返回 1,否则返回 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、isalnumisxdigit 的用法。函数 isdigit 确定其参数是否为数字 (0–9)。函数 isalpha 确定其参数是大写字母 (A-Z) 还是小写字母 (a–z)。函数 isalnum 确定其参数是大写字母、小写字母还是数字。函数 isxdigit 确定其参数是否为十六进制数字 (A–F, a–f,0–9)。

示例 1

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

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

   Serial.print (" hexadecimal digit
" );
   Serial.print (isxdigit( '$' ) ? "$ is a" : "$ is not a" );
   Serial.print (" hexadecimal digit
" );
   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

我们在每个函数中使用条件运算符 (?:) 来确定对于每个测试字符,输出中应打印字符串"is a"还是"is not a"。例如,行 a 表示如果 '8' 是数字(即,如果 isdigit 返回真(非零)值),则打印字符串"8 is a"。如果 '8' 不是数字(即,如果 isdigit 返回 0),则打印字符串"8 is not a"。

示例 2

以下示例演示了函数 islowerisupper 的使用。函数 islower 确定其参数是否为小写字母 (a–z)。函数 isupper 确定其参数是否为大写字母 (A–Z)。

int thisChar = 0xA0;

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

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

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

示例 3

以下示例演示了函数 isspace、iscntrl、ispunct、isprintisgraph 的使用。

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

  • 函数 iscntrl 确定其参数是否为控制字符,例如水平制表符 (' ')、垂直制表符 ('\v')、换页符 ('\f')、警告符 ('\a')、退格符 ('\b')、回车符 (' ') 或换行符 (' ')。

  • 函数 ispunct 确定其参数是否为控制字符,例如水平制表符 (' ')、垂直制表符 ('\v')、换页符 ('\f')、警告符 ('\a')、退格符 ('\b')、回车符 (' ') 或换行符 (' ')。

  • 函数 ispunct 确定其参数是否为是除空格、数字或字母之外的打印字符,例如 $、#、(、)、[、]、{、}、;、: 或 %。

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

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

void setup () {
   Serial.begin (9600);
   Serial.print ( " According to isspace:
Newline ") ;
   Serial.print (isspace( '
' )? " is a" : " is not a" );
   Serial.print ( " whitespace character
Horizontal tab") ;
   Serial.print (isspace( '	' )? " is a" : " is not a" );
   Serial.print ( " whitespace character
") ;
   Serial.print (isspace('%')? " % is a" : " % is not a" );
   
   Serial.print ( " 
According to iscntrl:
Newline") ;
   Serial.print ( iscntrl( '
' )?"is a" : " is not a" ) ;
   Serial.print (" control character
");
   Serial.print (iscntrl( '$' ) ? " $ is a" : " $ is not a" );
   Serial.print (" control character
");
   Serial.print ("
According to ispunct:
");
   Serial.print (ispunct(';' ) ?"; is a" : "; is not a" ) ;
   Serial.print (" punctuation character
");
   Serial.print (ispunct('Y' ) ?"Y is a" : "Y is not a" ) ;
   Serial.print ("punctuation character
");
   Serial.print (ispunct('#' ) ?"# is a" : "# is not a" ) ;
   Serial.print ("punctuation character
");

   Serial.print ( "
 According to isprint:
");
   Serial.print (isprint('$' ) ?"$ is a" : "$ is not a" );
   Serial.print (" printing character
Alert ");
   Serial.print (isprint('\a' ) ?" is a" : " is not a" );
   Serial.print (" printing character
Space ");
   Serial.print (isprint(' ' ) ?" is a" : " is not a" );
   Serial.print (" printing character
");
   
   Serial.print ("
 According to isgraph:
");
   Serial.print (isgraph ('Q' ) ?"Q is a" : "Q is not a" );
   Serial.print ("printing character other than a space
Space ");
   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