如何将JavaScript字符串值转换为所有小写字母?
示例:从“您的姓名”到“您的姓名”
如何将JavaScript字符串值转换为所有小写字母?
示例:从“您的姓名”到“您的姓名”
you can use the in built .toLowerCase() method on javascript strings. ex: var x = "Hello"; x.toLowerCase();
In case you want to build it yourself:
function toLowerCase(string) {
let lowerCaseString = "";
for (let i = 0; i < string.length; i++) {
//find ASCII charcode
let charcode = string.charCodeAt(i);
//if uppercase
if (charcode > 64 && charcode < 97){
//convert to lowercase
charcode = charcode + 32
}
//back to char
let lowercase = String.fromCharCode(charcode);
//append
lowerCaseString = lowerCaseString.concat(lowercase);
}
return lowerCaseString
}
Simply use JS toLowerCase()
let v = "Your Name"
let u = v.toLowerCase();
or
let u = "Your Name".toLowerCase();
Tray this short way
document.write((a+"").toUpperCase());
Method or Function: toLowerCase(), toUpperCase()
Description: These methods are used to cover a string or alphabet from lower case to upper case or vice versa. e.g: "and" to "AND".
Converting to Upper Case:- Example Code:-
<script language=javascript>
var ss = " testing case conversion method ";
var result = ss.toUpperCase();
document.write(result);
</script>
Result: TESTING CASE CONVERSION METHOD
Converting to Lower Case:- Example Code:
<script language=javascript>
var ss = " TESTING LOWERCASE CONVERT FUNCTION ";
var result = ss.toLowerCase();
document.write(result);
</script>
Result: testing lowercase convert function
Explanation: In the above examples,
toUpperCase() method converts any string to "UPPER" case letters.
toLowerCase() method converts any string to "lower" case letters.
Opt 1 : using toLowerCase();
var x = "ABC";
x=x.toLowerCase();
Opt 2 : Using your own function
function convertToLowerCase(str) {
var result = ''
for (var i = 0; i < str.length; i++) {
var code = str.charCodeAt(i)
if (code > 64 && code < 91) {
result += String.fromCharCode(code + 32)
} else {
result += str.charAt(i)
}
}
return result
}
Call it as:
x= convertToLowerCase(x);
Note that the function will ONLY work on STRING objects.
For instance, I was consuming a plugin, and was confused why I was getting a "extension.tolowercase is not a function" JS error.
onChange: function(file, extension)
{
alert("extension.toLowerCase()=>" + extension.toLowerCase() + "<=");
Which produced the error "extension.toLowerCase is not a function" So I tried this piece of code, which revealed the problem!
alert("(typeof extension)=>" + (typeof extension) + "<=");;
The output was"(typeof extension)=>object<=" - so AhHa, I was NOT getting a string var for my input. The fix is straight forward though - just force the darn thing into a String!:
var extension = String(extension);
After the cast, the extension.toLowerCase() function worked fine.
var lowerCaseName = "Your Name".toLowerCase();
Try
Demo - JSFiddle