檢查字元是否在 Arduino 中是字母數字
根據用例,您可能需要檢查字元在 Arduino 中是否為字母數字。一個示例可以是驗證密碼字串,在此類驗證中,您只允許密碼使用字母數字字元。或檢查 SD 卡中的檔名以便儲存在其中(有時候某些特殊字元不被允許在檔名中使用)。Arduino 有一個內建函式來檢查給定字元是否為字母數字。如您所猜想的那樣,該函式是isAlphaNumeric(),它將字元作為引數,並返回一個布林值。
示例
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial.println();
char c1 = 'a';
char c2 = '1';
char c3 = '%';
if (isAlphaNumeric(c1)) {
Serial.println("c1 is AlphaNumeric!");
} else {
Serial.println("c1 is NOT AlphaNumeric!");
}
if (isAlphaNumeric(c2)) {
Serial.println("c2 is AlphaNumeric!");
} else {
Serial.println("c2 is NOT AlphaNumeric!");
}
if (isAlphaNumeric(c3)) {
Serial.println("c3 is AlphaNumeric!");
} else {
Serial.println("c3 is NOT AlphaNumeric!");
}
}
void loop() {
// put your main code here, to run repeatedly:
}輸出
序列埠監視器輸出如下所示 −

您可看出,此函式按預期工作,針對字母和數字返回真,但針對特殊字元不返回真。您可以嘗試對其他字元執行此操作。
廣告
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP