猿问

如何使用JavaScript测试字符串中的字母是大写还是小写?

如何使用JavaScript测试字符串中的字母是大写还是小写?



米琪卡哇伊
浏览 1176回答 3
3回答

隔江千里

如果字符或整个字符串为数字,则josh和maleki的答案在大写和小写形式上都将返回true。使结果为假结果。使用乔什的例子var character = '5';if (character == character.toUpperCase()) {&nbsp;alert ('upper case true');}if (character == character.toLowerCase()){&nbsp;alert ('lower case true');}另一种方法是先测试它是否为数字,否则测试它是否为大写或小写示例var strings = 'this iS a TeSt 523 Now!';var i=0;var character='';while (i <= strings.length){&nbsp; &nbsp; character = strings.charAt(i);&nbsp; &nbsp; if (!isNaN(character * 1)){&nbsp; &nbsp; &nbsp; &nbsp; alert('character is numeric');&nbsp; &nbsp; }else{&nbsp; &nbsp; &nbsp; &nbsp; if (character == character.toUpperCase()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; alert ('upper case true');&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (character == character.toLowerCase()){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; alert ('lower case true');&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; i++;}

芜湖不芜

if (character == character.toLowerCase()){&nbsp; // The character is lowercase}else{&nbsp; // The character is uppercase}
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答