Home  >  Article  >  Web Front-end  >  How to convert string letters to uppercase in js

How to convert string letters to uppercase in js

青灯夜游
青灯夜游Original
2018-12-12 17:05:508763browse

The methods to convert string lowercase letters to uppercase letters in js are: use toUpperCase() method, use toLocaleUpperCase() method.

How to convert string letters to uppercase in js

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:

How to convert string letters to uppercase in js

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:

How to convert string letters to uppercase in js

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn