如何使用JavaScript测试字符串中的字母是大写还是小写?
当前回答
有一个非常简单的答案,其他人都没有提到过:
function isLowerCase(str) {
return str !== str.toUpperCase();
}
如果str. touppercase()没有返回相同的str,它必须是小写的。要测试大写,您可以将其更改为str !== str. tolowerercase()。
与其他一些答案不同,它可以正确地处理非字母字符(返回false),它也适用于其他字母,重音字符等。
其他回答
function checkCharType (charToCheck) {
// body...
var returnValue = "O";
var charCode = charToCheck.charCodeAt(0);
if(charCode >= "A".charCodeAt(0) && charCode <= "Z".charCodeAt(0)){
returnValue = "U";
}else if (charCode >= "a".charCodeAt(0) &&
charCode <= "z".charCodeAt(0) ){
returnValue = "L";
}else if (charCode >= "0".charCodeAt(0) &&
charCode <= "9".charCodeAt(0) ) {
returnValue = "N";
}
return returnValue;
}
var myString = prompt("Enter Some text: ", "Hello world !");
switch (checkCharType(myString)) {
case "U":
// statements_1
document.write("First character was upper case");
break;
case "L":
document.write("First character was a lower case");
break;
case "N":
document.write("First character was a number");
break
default:
// statements_def
document.write("First character was not a character or a number");
break;
}
Define a Function checkCharType().By declaring the variable returnValue and initialising it to the Character "O" to indicate it's Some other value. U for uppercase; L for Lowercase ; N for number Use the charCodeAt() method to get the character code of the first character. Using if Statement , which check within what range of values the character code falls. If it falls between the character codes for A and Z, Its Uppercase, character code between a and z ,Its Lowercase. and so on. "A".charCode(0) var myChar = new String("A"); myChar.charCodeAt(0); "A" : number code "65“ Check the String
function isCapital(ch){
return ch.charCodeAt() >= 65 && ch.charCodeAt() <= 90;
}
最好的方法是使用正则表达式、三元运算符和内置的字符串.test()方法。
我把正则表达式的里里外外和字符串的测试方法留给你,但在这里我们将使用它来测试你的变量。
/[a-z]/i.test(your-character-here)
这将根据您的字符是否匹配正则表达式中的字符集返回TRUE或FALSE。由于i标志,我们的正则表达式检查所有字母a-z /[a-z]/,而不管它们的大小写。
所以,一个基本的测试是:
var theAnswer = "";
if (/[a-z]/i.test(your-character-here)) {
theAnswer = "It's a letter."
}
现在我们需要确定它是大写还是小写。因此,如果我们从正则表达式中删除i标志,那么上面的代码将测试小写字母a-z。如果我们在第一个if语句的else中插入另一个if语句,我们也可以用A-Z测试大写。是这样的:
var theAnswer = "";
if (/[a-z]/.test(your-character-here)) {
theAnswer = "It's a lower case letter."
} else if (/[A-Z]/.test(your-character-here)) {
theAnswer = "It's an upper case letter.";
}
以防它不是字母,我们可以添加一个最后的else语句:
var theAnswer = "";
if (/[a-z]/.test(your-character-here)) {
theAnswer = "It's a lower case letter."
} else if (/[A-Z]/.test(your-character-here)) {
theAnswer = "It's an upper case letter.";
} else {
theAnswer = "It's not a letter."
}
上面的代码可以工作。但它有点丑。相反,我们可以使用“三元运算符”来替换上面的if-else语句。三元运算符只是简单的if-else编码方式。语法很简单:
(statement-to-be-evaluated) ? (code-if-true) : (code-if-false)
这些也可以相互嵌套。所以函数可能是这样的:
var theAnswer = "";
function whichCase(theLetter) {
theAnswer = /[a-z]/.test(theLetter) ? "It's lower case." : "";
theAnswer = /[A-Z]/.test(theLetter) ? "It's upper case." : "";
return(theAnswer);
}
上面的代码看起来很好,但不能完全工作,因为如果我们的字符是小写的,当它测试大写时,答案会被设置为“”,所以让它们嵌套:
var theAnswer = "";
function whichCase(theLetter) {
theAnswer = /[a-z]/.test(theLetter) ? "It's lower case." : (/[A-Z]/.test(theLetter) ? "It's upper case." : "It's not a letter.");
return(theAnswer);
}
那会很有用的!但是没有必要有两个单独的行来设置变量theAnswer,然后返回它。我们应该使用let和const而不是var(如果你不确定为什么,请查阅它们)。一旦我们做出这些改变:
function whichCase(theLetter) {
return(/[A-Z]/.test(theLetter) ? "It's upper case." : (/[a-z]/.test(theLetter) ? "It's lower case." : "It's not a letter."));
}
我们最终得到了一段优雅、简洁的代码。;)
Stephen Nelsons的函数转换成带有大量测试示例的原型。
为了完整起见,我还在函数中添加了整个字符串。
有关其他注释,请参阅代码。
/* Please note, there's no requirement to trim any leading or trailing white spaces. This will remove any digits in the whole string example returning the correct result. */ String.prototype.isUpperCase = function(arg) { var re = new RegExp('\\s*\\d+\\s*', 'g'); if (arg.wholeString) {return this.replace(re, '') == this.replace(re, '').toUpperCase()} else return !!this && this != this.toLocaleLowerCase(); } console.log('\r\nString.prototype.isUpperCase, whole string examples'); console.log(' DDD is ' + ' DDD'.isUpperCase( { wholeString:true } )); console.log('9 is ' + '9'.isUpperCase( { wholeString:true } )); console.log('Aa is ' + 'Aa'.isUpperCase( { wholeString:true } )); console.log('DDD 9 is ' + 'DDD 9'.isUpperCase( { wholeString:true } )); console.log('DDD is ' + 'DDD'.isUpperCase( { wholeString:true } )); console.log('Dll is ' + 'Dll'.isUpperCase( { wholeString:true } )); console.log('ll is ' + 'll'.isUpperCase( { wholeString:true } )); console.log('\r\nString.prototype.isUpperCase, non-whole string examples, will only string on a .charAt(n) basis. Defaults to the first character'); console.log(' DDD is ' + ' DDD'.isUpperCase( { wholeString:false } )); console.log('9 is ' + '9'.isUpperCase( { wholeString:false } )); console.log('Aa is ' + 'Aa'.isUpperCase( { wholeString:false } )); console.log('DDD 9 is ' + 'DDD 9'.isUpperCase( { wholeString:false } )); console.log('DDD is ' + 'DDD'.isUpperCase( { wholeString:false } )); console.log('Dll is ' + 'Dll'.isUpperCase( { wholeString:false } )); console.log('ll is ' + 'll'.isUpperCase( { wholeString:false } )); console.log('\r\nString.prototype.isUpperCase, single character examples'); console.log('BLUE CURAÇAO'.charAt(9) + ' is ' + 'BLUE CURAÇAO'.charAt(9).isUpperCase( { wholeString:false } )); console.log('9 is ' + '9'.isUpperCase( { wholeString:false } )); console.log('_ is ' + '_'.isUpperCase( { wholeString:false } )); console.log('A is ' + 'A'.isUpperCase( { wholeString:false } )); console.log('d is ' + 'd'.isUpperCase( { wholeString:false } )); console.log('E is ' + 'E'.isUpperCase( { wholeString:false } )); console.log('À is ' + 'À'.isUpperCase( { wholeString:false } )); console.log('É is ' + 'É'.isUpperCase( { wholeString:false } )); console.log('Ñ is ' + 'Ñ'.isUpperCase( { wholeString:false } )); console.log('ñ is ' + 'ñ'.isUpperCase( { wholeString:false } )); console.log('Þ is ' + 'Þ'.isUpperCase( { wholeString:false } )); console.log('Ͻ is ' + 'Ͻ'.isUpperCase( { wholeString:false } )); console.log('Ͽ is ' + 'Ͽ'.isUpperCase( { wholeString:false } )); console.log('Ά is ' + 'Ά'.isUpperCase( { wholeString:false } )); console.log('Έ is ' + 'Έ'.isUpperCase( { wholeString:false } )); console.log('ϴ is ' + 'ϴ'.isUpperCase( { wholeString:false } )); console.log('Ϋ is ' + 'Ϋ'.isUpperCase( { wholeString:false } )); console.log('Ϣ is ' + 'Ϣ'.isUpperCase( { wholeString:false } )); console.log('Ϥ is ' + 'Ϥ'.isUpperCase( { wholeString:false } )); console.log('Ϧ is ' + 'Ϧ'.isUpperCase( { wholeString:false } )); console.log('Ϩ is ' + 'Ϩ'.isUpperCase( { wholeString:false } )); console.log('Ϫ is ' + 'Ϫ'.isUpperCase( { wholeString:false } )); console.log('Ϭ is ' + 'Ϭ'.isUpperCase( { wholeString:false } )); console.log('Ϯ is ' + 'Ϯ'.isUpperCase( { wholeString:false } )); console.log('Ϲ is ' + 'Ϲ'.isUpperCase( { wholeString:false } )); console.log('Ϸ is ' + 'Ϸ'.isUpperCase( { wholeString:false } )); console.log('Ϻ is ' + 'Ϻ'.isUpperCase( { wholeString:false } ));
function solution(s) {
var c = s[0];
if (c == c.toUpperCase() && !(c >= '0' && c <= '9') &&(c >='A' && c <= 'Z')) {
return 'upper';
} else if (c == c.toLowerCase() && !(c >= '0' && c <= '9') &&(c >='a' && c <= 'z')){
return 'lower';
} else if (c >= '0' && c <= '9'){
return 'digit'
} else {
return 'other'
}
}
var str1= (solution('A')) // upper
var str2 = solution('b') // lower
var str3 = solution('1') // digit
var str4 = solution('_') // other
console.log(`${str1} ${str2} ${str3} ${str4}`)