We call a sequence of characters a string. This is one of the basic types found in almost all programming languages.
Here we show you 10 great tips about JS strings.
1. How to duplicate a string multiple times
JS strings allow simple repetition. Unlike copying a string purely by hand, we can use the string REPEAT method.
2. how to fill a string to the specified length
Sometimes we want the string to have a specific length. If the string is too short, the remaining space needs to be filled until the specified length is reached.
In the past, the library left-pad was mainly used, but today we can use the
SpadEndmethods, and the choice depends on whether the string is filled at the beginning or the end of the string.
3. How to split a string into an array of characters
There are several ways to split a string into an array of characters, I prefer to use the extension operator (…) .
Note that this does not always work as expected. For more information, see the next tip.
4. How to count the characters in a string
You can use the length property.
But for Chinese, this method is less reliable.
𩸽returns length of 2. Why? JS represents most characters as 16-bit code points.
However, some characters are represented as two (or more) 16-bit code points, called proxy pairs. If the length attribute is used, JS tells you how many code points are being used. Thus,
𩸽 (hokke) consists of two code points and returns the wrong value.
So how do you go about determining this, using the deconstruct operation notation (…) .
This method works in most cases, but there are some extreme cases. For example, if emojis are used, then sometimes this length is also wrong. If you really want to calculate the correct length of a character, you must break the word into Grapheme Clusters, which is beyond the scope of this article and will not be described here.
5. How to invert the characters in a string
As before, there are some edge cases. When encountering edge cases it is necessary to first split the word into grapheme clusters.
6. How to capitalize the first letter in a string
A very common operation is to capitalize the first letter of a string. While many programming languages have a native way to do this, JS needs to do some work.
7. How to split a string over multiple separators
Suppose we want to split strings on separators, the first thing that comes to mind is to use the split method, which you surely know. However, one thing you may not know is that split can split multiple delimiters at the same time, which can be achieved by using regular expressions.
8. How to check if a string contains a specific sequence
String searching is a common task. In JS, you can easily do this using the String.includes method. No regular expressions are required.
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.
10. How to replace all occurrences of the string
There are multiple ways to replace all occurrences of a string. You can use the String.replace method and regular expressions with global flags. Or, you can use the new String.replaceAll method. Please note that this new method is not available in all browsers and Node.js versions.