JavaScript string tricks (reverse, split, replace...)

Strings are one of the fundamental types in almost all programming languages. Summarized the commonly used skills, so let's start now.

1. How to copy a string multiple times

JS strings allow simple repetition. Unlike copying strings by hand, we can use the method of string repetition.

const str_repeat = 'MoMl'.repeat(3)
consol.log(str_repeat) // "MoMlMoMlMoMl"
2. How to pad a string to a specified length

Sometimes we want strings to have a specific length. If the string is too short, the remaining space needs to be padded until the specified length is reached.

We can use the padStart and padEnd methods, the choice depends on whether the string is padded at the beginning or end of the string.

// Add "0" to the beginning until the length of the string is 8.
const str_padStart = '001'.padStart(8, '0')
console.log(str_padStart) // "00000001"

//Add " *" at the end until the length of the string is 5.
const str_padEnd = "34".padEnd(5, "*")
console.log(str_padEnd) // "34***"
3. How to split a string into an array of characters

There are several ways to split a string into an array of characters, such as using the spread operator (...)

const word = 'MoMl'
const cut_word = [...word]
console.log(cut_word)

You can also use str.split("")

const word = 'MoMl'
const cut_word = word.split("")
console.log(cut_word)
4. How to count characters in a string

The length attribute can be used.

const word = "MoMl";
console.log(word.length) // 4
5. How to reverse characters in a string

Reversing the characters in a string is as easy as combining the spread operator (...), the Array.reverse method, and the Array.join method.

const word = "MoMl"
const reversed_word = [...word].reverse().join("")
console.log(reversed_word) // "lMoM"
6. How to capitalize the first letter in a string
let word = 'momo'
// word.substr(1):截取首字母后的字符
word = word[0].toUpperCase() + word.substr(1)
console.log(word) // "Momo"

You can also:

let word = "momo";
const characters = [...word];
characters[0] = characters[0].toUpperCase();
word = characters.join("");
console.log(word); // "Momo"
7. How to split a string on multiple delimiters

Suppose we want to split a string on a delimiter, the first thing we think of is to use the split method, which of course is known to smart people. However, what you may not know is that split can split on multiple delimiters at the same time , which can be achieved by using regular expressions.

const list = "apples,bananas;cherries"
const fruits = list.split(/[,;]/)
console.log(fruits); // ["apples", "bananas", "cherries"]
8. How to check if a string contains a specific sequence

String searching is a common task, in JS you can do it easily with String.includes method, no regex required.

const text = "Hello, world! My name is MoMo"
console.log(text.includes("MoMo")); // true
9. How to check if a string starts or ends with a specific sequence

To search at the beginning or end of a string, you can use the String.startsWith and String.endsWith methods.

const text = "Hello, world! My name is MoMo!"
console.log(text.startsWith("Hello")); // true
console.log(text.endsWith("world")); // false
10. How to replace all occurrences of a string

There are several ways to replace all occurrences of a string, you can use the String.replace method with regular expressions with global flags ; or use the new String.replaceAll method, please note that this new method is not available in all browsers and Available in all Node.js versions.

const text = "Hello, world! Hello, world!!"
console.log(text.replace(/world/g, "today"));
// "Hello, today! Hello, today!!"
console.log(text.replaceAll("world", "today"));

To be continued~~~

Guess you like

Origin blog.csdn.net/m0_65489440/article/details/128953149