如何使用JavaScript测试字符串中的字母是大写还是小写? [英] How can I test if a letter in a string is uppercase or lowercase using JavaScript?
本文介绍了如何使用JavaScript测试字符串中的字母是大写还是小写?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何使用JavaScript测试字符串中的字母是大写还是小写?
How can I test if a letter in a string is uppercase or lowercase using JavaScript?
推荐答案
josh的回答如果字符或整个字符串是数字,则maleki将在大写和小写两种情况下返回true。使结果成为错误的结果。
使用josh的例子
The answer by josh and maleki will return true on both upper and lower case if the character or the whole string is numeric. making the result a false result. example using josh
var character = '5';
if (character == character.toUpperCase()) {
alert ('upper case true');
}
if (character == character.toLowerCase()){
alert ('lower case true');
}
另一种方法是先测试它是否为数字,否则测试大写或小写
示例
another way is to test it first if it is numeric, else test it if upper or lower case example
var strings = 'this iS a TeSt 523 Now!';
var i=0;
var character='';
while (i <= strings.length){
character = strings.charAt(i);
if (!isNaN(character * 1)){
alert('character is numeric');
}else{
if (character == character.toUpperCase()) {
alert ('upper case true');
}
if (character == character.toLowerCase()){
alert ('lower case true');
}
}
i++;
}
这篇关于如何使用JavaScript测试字符串中的字母是大写还是小写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文