Home >Web Front-end >JS Tutorial >How to convert a specified string to lowercase letters in js

How to convert a specified string to lowercase letters in js

青灯夜游
青灯夜游Original
2018-12-12 15:59:052710browse

In JavaScript, you can use the toLowerCase() method and toLocaleLowerCase() method to convert uppercase letters in a string to lowercase letters.

How to convert a specified string to lowercase letters in js

Use toLowerCase() method

The toLowerCase() method of string can be Convert the letters in the string to lowercase letters, and a string of lowercase letters will be returned.

Note: The toLowerCase() method will not make any other changes to the original string; it will not affect non-alphabetic characters.

Syntax:

string.toLowerCase()

string is the string object that needs to be converted.

Simple example: Use the toLowerCase() method to convert the string "PHP Chinese website's website is: WWW.PHP.CN" into lowercase letters.

<div class="demo ">
	<p>转换前:<br />
		<span id="str1"></span>
	</p>
	<p>转换后:<br />
	<span id="str2"></span>
	</p>
 
</div>

<script type="text/javascript">
var str1 ="PHP中文网的网站为:WWW.PHP.CN";
var str2 =str1.toLowerCase();

document.getElementById("str1").innerHTML =str1;
document.getElementById("str2").innerHTML =str2;
</script>

Rendering:

How to convert a specified string to lowercase letters in js

As can be seen from the above example, the string can be easily converted to lowercase letters using the toLowerCase() method; but Sometimes string conversion to lowercase may vary due to conflicts between different languages.

Example: The uppercase representation of the lowercase letter 'i' in different languages ​​is different. In English, it is represented by the uppercase letter 'I'; while in Turkish, it is represented by the uppercase dotted letter '? 'express.

So how to overcome this problem? We can use toLocaleLowerCase() method to convert lower case.

Using the toLocaleLowerCase() method

The toLocaleLowerCase() method of a string will convert the string to the corresponding locale according to the current locale of the user's computer. of lowercase letters will also return a string of lowercase letters.

Syntax:

string.toLocaleLowerCase()

Simple example: Use toLocaleLowerCase() method to convert the string "ABCDEfx".

<div class="demo ">
	<p>转换前:<br /><br />
		<span id="str1"></span>
	</p><br />
	<p>转换后:<br /><br />
	<span id="str2"></span>
	</p>
 
</div>

<script type="text/javascript">
var str1 ="ABCDEfx";
var str2 =str1.toLocaleLowerCase();

document.getElementById("str1").innerHTML =str1;
document.getElementById("str2").innerHTML =str2;
</script>

Rendering:

How to convert a specified string to lowercase letters in js

Description:

In most cases , the results obtained by using the toLocaleLowerCase() method or the toLowerCase() method are the same, and there will be different results only when the rules defined by the locale conflict with the regular Unicode case mapping method.

Summary: 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 a specified string to lowercase letters 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