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

How to convert string to uppercase in es6

青灯夜游
青灯夜游Original
2023-02-15 11:56:482433browse

Conversion method: 1. Use the toUpperCase() function to convert the string to uppercase, the syntax "string.toUpperCase()"; 2. Use the toLocaleUpperCase() function to convert the string to uppercase, the syntax "string. toLocaleUpperCase()"; 3. Use slice(), toUpperCase(), toLowerCase() functions and the string splicing character " " to set the first letter to be capitalized.

How to convert string to uppercase in es6

The operating environment of this tutorial: Windows 7 system, ECMAScript version 6, Dell G3 computer.

Method 1. Use the toUpperCase() function to convert the string to uppercase

The function of the toUpperCase() method: convert the specified string into uppercase Convert all lowercase letters to uppercase letters, and finally return a new string.

Syntax

stringObject.toUpperCase()
  • will return a new string in which all lowercase characters of stringObject are converted to uppercase characters.

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>

How to convert string to uppercase in es6

##Method 2. Use toLocaleUpperCase() function to Convert a string to uppercase

The function of toLocaleUpperCase(): it will convert all the lowercase letters in the specified string to uppercase letters according to the user's current computer locale, and finally return a new string.

Syntax

stringObject.toLocaleUpperCase()

  • will return a new string in which all lowercase characters of stringObject are converted to uppercase characters.

Explanation

Different from toUpperCase(), the toLocaleUpperCase() method converts the string to uppercase according to the local method. Only a few languages ​​(such as Turkish) have local case mapping, so the return value of this method is usually the same as toUpperCase(). But in most cases, the toLocaleUpperCase() method returns the same result as the toUpperCase() method.

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>

How to convert string to uppercase in es6

Summary : It can be seen that both the toUpperCase() method and the toLocaleUpperCase() method will only change the lowercase letters of the specified string and convert them to uppercase letters, and will not make any changes to other non-letters (or uppercase letters).

Extended knowledge: Capitalize the first letter

There is no function to capitalize the first letter in javascript. But we can use slice(), toUpperCase(), toLowerCase() functions and the string splicing character " " to set the first letter to be capitalized.

  • Use the slice() method to divide the string into two parts: the initial character part, and other sub-character parts.

  • Use the toUpperCase() method to convert the first letter to uppercase; use toLowerCase() to convert other subcharacters to lowercase.

  • Use the " " operator to rejoin the two parts

  • function f(str) {
    newStr = str.slice(0,1).toUpperCase() +str.slice(1).toLowerCase();
       console.log(newStr);
    }
    f("hello World!");

How to convert string to uppercase in es6

[Recommended learning :

javascript video tutorial

The above is the detailed content of How to convert string to uppercase in es6. 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