如何测试字符是数字,大写还是小写?

时间:2012-06-20 20:23:34

标签: javascript

我正在尝试运行这一点JavaScript。我不断收到错误消息:

ThisChar.toUpperCase() is not a function 

我尝试过使用jsfiddle.net但似乎无法解决此错误。这是怎么回事?

// CREATE ARRAY OF CHARACTERS
CharacterArray = ["1", "2", "3", "a", "b", "c", "D", "E", "F", "/", "+", ")"];

// TEST CHARACTERS 
function testCharacters() {
   // GET ARRAY LEN
   var CharacterArrayLen = CharacterArray.length;
   // LOOP THROUGH ARRAY
   for (i = 0; i < CharacterArrayLen; i++) {
       // PARSE SINGLE CHARACTER
       var ThisChar = CharacterArray.slice(i, i + 1);
       if (!isNaN(ThisChar * 1)) {
           alert(ThisChar + ' is numeric!');
       } else {
           if (ThisChar == ThisChar.toUpperCase()) {
               alert(ThisChar + ' is upper case true');
           } else if (ThisChar == ThisChar.toLowerCase()) {
               alert(ThisChar + " is lower case");
           } else {
               alert(ThisChar + " is unknown");
           }
       }
   }
}
testCharacters();​

1 个答案:

答案 0 :(得分:6)

.slice总是返回一个数组。只需访问元素:

var ThisChar = CharacterArray[i];

您不必将值乘以*1,传递给isNaN的值将隐式转换为数字:

!isNaN(ThisChar)