Home >Web Front-end >JS Tutorial >How to convert string letters to uppercase in js
The methods to convert string lowercase letters to uppercase letters in js are: use toUpperCase() method, use toLocaleUpperCase() method.
Use toUpperCase() method
Function: Change the specified string All lowercase letters in are converted to uppercase letters, and finally a new string is returned. [Recommended related video tutorials: JavaScript tutorial]
Grammar:
string.toUpperCase()
Simple example:
<div class="demo "> <p>转换前:<br /> <span id="str1"></span> </p><br /> <p>转换后:<br /> <span id="str2"></span> </p> </div> <script type="text/javascript"> var str1 ="php中文网的网址为:www.php.cn!"; var str2 =str1.toUpperCase(); document.getElementById("str1").innerHTML =str1; document.getElementById("str2").innerHTML =str2; </script>
Rendering:
Use toLocaleUpperCase() method
Function : All lowercase letters in the specified string will be converted to uppercase letters according to the locale of the user's current computer, and finally a new string will be returned.
Note: In most cases, the results returned by the toLocaleUpperCase() method are the same as those of the toUpperCase() method.
Syntax:
string.toLocaleUpperCase()
Simple example: Use toLocaleUpperCase() method to convert the string "uppercase letter conversion of JavaScript string!"
<div class="demo "> <p>转换前:<br /> <span id="str1"></span> </p><br /> <p>转换后:<br /> <span id="str2"></span> </p> </div> <script type="text/javascript"> var str1 ="JavaScript字符串的大写字母转换!"; var str2 =str1.toLocaleUpperCase(); document.getElementById("str1").innerHTML =str1; document.getElementById("str2").innerHTML =str2; </script>
Rendering:
Summary: It can be seen that whether it is the toUpperCase() method or the toLocaleUpperCase() method, both Only the lowercase letters of the specified string will be changed and converted to uppercase letters. No changes will be made to other non-letters (or uppercase letters).
The above is the entire content of this article, I hope it will be helpful to everyone's study.
The above is the detailed content of How to convert string letters to uppercase in js. For more information, please follow other related articles on the PHP Chinese website!