Question: How to change the value or length of a certain bit of a string in JavaScript? Change the value of a certain bit: use charAt() to get the character, and then use replace() to replace it. Change the length: intercept (slice), append (concat) or remove (slice/replace) the string.
How to change the value or length of a certain bit of a string in JavaScript
Change the string The value of a certain bit
Use the charAt()
method to get the character at the specified position in the string, and then use the replace()
method to replace the character. For example:
<code>const str = "Hello"; const newStr = str.replace("l", "L"); console.log(newStr); // 输出 "HeLLo"</code>
Change the string length
To change the string length, you can use the following method:
slice()
method to intercept a part of the string. concat()
method to append one or more strings to an existing string. slice()
or replace()
method to remove characters from the string. Example:
<code>const str = "Hello World"; const newStr = str.slice(0, 5); console.log(newStr); // 输出 "Hello"</code>
<code>const str = "Hello"; const newStr = str.concat(" World"); console.log(newStr); // 输出 "Hello World"</code>
<code>const str = "Hello World"; const newStr = str.replace("World", ""); console.log(newStr); // 输出 "Hello"</code>
The above is the detailed content of How to change the size and length of a certain bit of a string in js. For more information, please follow other related articles on the PHP Chinese website!