Arduino-character-functions

提供:Dev Guides
移動先:案内検索

Arduino-キャラクター関数

すべてのデータは、文字、数字、さまざまな特殊記号を含む文字としてコンピューターに入力されます。 このセクションでは、個々の文字を調べて操作するためのC ++の機能について説明します。

文字処理ライブラリには、文字データの有用なテストと操作を実行するいくつかの関数が含まれています。 各関数は、intとして表される文字、または引数としてEOFを受け取ります。 文字はしばしば整数として操作されます。

EOFの値は通常–1であり、一部のハードウェアアーキテクチャでは負の値をchar変数に格納できないことを思い出してください。 したがって、文字処理関数は文字を整数として操作します。

次の表は、文字処理ライブラリの機能をまとめたものです。 文字処理ライブラリの関数を使用する場合、 <cctype> ヘッダーを含めます。

S.No. Prototype & Description
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が16進数字の場合は1を返し、そうでない場合は0を返します。

(2進数、8進数、10進数、16進数の詳細な説明については、付録D「番号システム」を参照してください。)

5

int islower( int c )

cが小文字の場合は1を返し、そうでない場合は0を返します。

6

int isupper( int c )

cが大文字の場合、1を返します。それ以外の場合は0。

7

int isspace( int c )

cが空白文字(改行( '\ n')、スペース)の場合、1を返します

)、フォームフィード( '\ f')、キャリッジリターン( '\ r')、水平タブ( '\ t')、または垂直タブ( '\ v')—それ以外の場合は0。

8

int iscntrl( int c )

cが改行( '\ n')、フォームフィード( '\ f')、復帰( '\ r')、水平タブ( '\ t')、垂直タブ( 'などの制御文字の場合、1を返します。 \ 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 は、引数が16進数(A〜F、a〜f、0〜9)であるかどうかを判別します。

例1

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

各関数で条件演算子*(?:)を使用して、文字列 "が"であるか、文字列 "が非"であるかを、テストする各文字の出力に出力する必要があるかどうかを判断します。 たとえば、行 *a は、「8」が数字の場合、つまり isdigit が真(ゼロ以外)の値を返す場合、文字列「8 is a」が出力されることを示します。 「8」が数字でない場合(つまり、 isdigit が0を返す場合)、文字列「8 is not a」が出力されます。

例2

次の例は、関数 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

実施例3

次の例は、関数 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