Home >Web Front-end >JS Tutorial >How to unify the upper and lower case of all characters in JS string learning

How to unify the upper and lower case of all characters in JS string learning

青灯夜游
青灯夜游Original
2021-08-17 17:01:241993browse

In the previous article, we introduced the method of intercepting a substring according to a given length and obtaining the file extension. If you are interested, you can click on the link to read → "JS string learning method by intercepting substrings Return file extension 》. This time we continue to learn JavaScript strings and see how to unify the upper and lower case of strings.

Sometimes we get a string with mixed case, which is inconvenient to read. So how to unify the case of the string? This requires string case conversion to uniformly convert them to uppercase or lowercase. Let’s learn more about it in detail below.

Let’s take a look at such an example:

var str="THE quick brown FOX JUMPS OVER the lazy DOG";
console.log(str.toLowerCase());
console.log(str.toUpperCase());
console.log(str.toLocaleLowerCase());
console.log(str.toLocaleUpperCase());

The output result is:

How to unify the upper and lower case of all characters in JS string learning

It can be seen that: Both toLowerCase() and toLocaleLowerCase() functions can convert strings to lowercase; and toUpperCase() and toLocaleUpperCase() can both convert strings to uppercase.

But there are still some differences:

toLocaleLowerCase() and toLocaleUpperCase() are two localized prototype methods that convert string case to upper and lower case according to the local host's locale.

Normally, the return value will be the same as the toLowerCase(), toUpperCase() method; but in some special languages ​​(such as Turkish), because they have local-specific case mapping, the return value will be Sometimes it's different.

We generally only need to use the toLowerCase() and toUpperCase() methods to set uppercase strings and lowercase strings. Let’s take a look at these two methods:

  • toLowerCase() function is used to convert a string to lowercase, the syntax is “string.toLowerCase()” , you can directly convert all characters in the string string to lowercase, and then return a new string containing all lowercase characters.

  • toUpperCase() function is used to convert a string to uppercase, the syntax is "string.toUpperCase()", you can directly convert the string string Convert all characters in to uppercase, then return a new string containing all uppercase characters.

The toLowerCase() and toUpperCase() methods will return a new string and will not change the original string.

var str="THE quick brown FOX JUMPS OVER the lazy DOG";
console.log(str.toLowerCase());
console.log("原字符串:"+str);
console.log(str.toUpperCase());
console.log("原字符串:"+str);

The output result is:

How to unify the upper and lower case of all characters in JS string learning

Okay, that’s all. If you need it, you can read: javascript advanced tutorial

The above is the detailed content of How to unify the upper and lower case of all characters in JS string learning. 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