Get the App
SLTechnology News&Howtos  ›  Development  › 

How es6 replaces the first character of a string with another character

Shulou Source: shulou.com Published: 2022-05-31 16:38:38 11月14日 Update

This article mainly explains "es6 how to replace the first character of a string with other characters". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "es6 how to replace the first character of a string with other characters".

Replacement method: 1, use charAt () to get and return the first character in the string, syntax "string object .charat (0)"; 2, use replace () to replace the first character acquired with other characters, syntax "string object .replace (first character value," new character ")".

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

Es6's method of replacing the first character of a string with another character

1. Use charAt () to get the first character in the string

The charAt () method returns the characters at the specified position. Syntax:

StringObject.charAt (index)

Index is required. A number that represents a position in a string, that is, the subscript of a character in a string.

The subscript of the first character in the string is 0. If the parameter index is not between 0 and string.length, the method returns an empty string.

Var str= "t679njhsimmkh"; var zf=str.charAt (0); console.log (str); console.log (zf)

2. Replace the first character acquired with replace ()

The replace () method is used to replace some characters in a string with other characters, or to replace a substring that matches a regular expression.

Str.replace (zf, "CBA")

Thank you for your reading, the above is the content of "how es6 replaces the first character of a string with other characters". After the study of this article, I believe you have a deeper understanding of how es6 replaces the first character of a string with other characters, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Characters strings methods grammar learning subscript location content objects use between parameters that is ideas situations tutorials numbers articles new words more Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Xiaomi Shulou Technology Docker Shulou Information