如何利用 javascript 測試字串中的某個字母是大寫還是小寫?


要利用 javascript 測試字串中的某個字母是大寫還是小寫,你可以簡單地將該字元轉換為相應的大小寫,然後檢視結果。

示例

function checkCase(ch) {
   if (!isNaN(ch * 1)){
      return 'ch is numeric';
   }
    else {
      if (ch == ch.toUpperCase()) {
         return 'upper case';
      }
      if (ch == ch.toLowerCase()){
         return 'lower case';
      }
   }
}
console.log(checkCase('a'))
console.log(checkCase('A'))
console.log(checkCase('1'))

輸出

結果為 -

lower case
upper case
ch is numeric

更新於: 02-12-2019

7K+ 瀏覽

開啟你的 事業

完成課程以獲得認證

開始
廣告
© . All rights reserved.