檢查字元是否在 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:
}

輸出

序列埠監視器輸出如下所示 −

您可看出,此函式按預期工作,針對字母和數字返回真,但針對特殊字元不返回真。您可以嘗試對其他字元執行此操作。

更新於:2021 年 5 月 31 日

647 次瀏覽量

開啟您的 職業生涯

完成課程可獲得認證

立即開始
廣告
© . All rights reserved.