- How to Copy a String Multiple Times
- How to Pad a String to a Specific Length
- How to Split a String Into an Array of Characters
- How to Count Characters in a String
- How to Reverse Characters in a String
- How to Capitalize (Uppercase) the First Letter in a String
- How to Split a String on Multiple Separators
- How to Check If a String Contains a Specific Sequence
- How to Check If a String Starts or Ends With a Specific Sequence
- How to Replace All Occurrences of a String
1. How to Copy a String Multiple Times
// Concatenate "ha" 3 times. const laughing = "ha".repeat(3); console.log(laughing); // "hahaha" // Concatenate "1" 8 times. const eightBits = "1".repeat(8); console.log(eightBits ); // "11111111"
2. How to Pad a String to a Specific Length
Sometimes, you want your string to have a specific length. If your string is too short, you’d like to fill the remaining space until it reaches your specific length. In the past, people often used libraries for this. This had negative consequences with the notorious
left-pad incident. But, today you can use
String#padEnd. Which method you choose depends on whether you want to pad your string at the beginning or the end.
// Add "0" to the beginning until the string has a length of 8. const eightBits = "001".padStart(8, "0"); console.log(eightBits); // "00000001" // Add "*" to the end until the string has a length of 5. const anonymizedCode = "34".padEnd(5, "*"); console.log(anonymizedCode); // "34***"
3. How to Split a String Into an Array of Characters
There are multiple ways to split a string into an array of characters. I prefer using the spread-Operator (
const word = "apple"; const characters = [...word]; console.log(characters); // ["a", "p", "p", "l", "e"]
Note, that this does not always work as intended. See the next tip for more information.
4. How to Count Characters in a String
Easy. You can use the
const word = "apple"; console.log(word.length); // 5
But, wait a minute! Sometimes, this shows some strange behavior. Look at the following example:
const word = "𩸽"; console.log(word.length); // 2
Weird! The japanese kanji hokke returns a
Can we do something about it? Well, yes. The fellow spread-operator (
...) saves our day, again.
const word = "𩸽"; const characters = [...word]; console.log(characters.length) // 1;
But, this is not the full story. It works most of the time, but there are edge cases. For example, if you are working with emoji, sometimes also this length is false. If you really want to count the human-perceived characters, then you have to split your word into grapheme clusters. Unfortunately, this is beyond the scope of this article.
5. How to Reverse Characters in a String
Reversing the characters in a string is easily done. Simply combine the spread-Operator (
Array#reverse method and the
const word = "apple"; const reversedWord = [...word].reverse().join(""); console.log(reversedWord); // "elppa"
Again, like in the previous tip there are some rare edge cases. It might be possible that you have to split your word into grapheme clusters first. Again, this is beyond the scope of this article. Sorry!
6. How to Capitalize (Uppercase) the First Letter in a String
let word = "apple"; // Concatenate capitalized first character with remaining word. word = word.toUpperCase() + word.substr(1); console.log(word); // "Apple"
// This shows an alternative way let word = "apple"; // Use spread operator (`...`) to split into characters. // Transform first character and join array. const characters = [...word]; characters = characters.toUpperCase(); word = characters.join(""); console.log(word); // "Apple"
7. How to Split a String on Multiple Separators
Let's say you want to split a string on a separator. Therefore, you can use the
String#split method. You probably know this. However, did you know you could split on multiple separators at the same time? This is possible using a regular expression:
// Let's split on comma (,) and semicolon (;). 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#includes method. No regular expression needed.
const text = "Hello, world! My name is Kai!" console.log(text.includes("Kai")); // true
9. How to Check If a String Starts or Ends With a Specific Sequence
Similar to #8, you can search at the beginning or the end of a string. For this you can use the
const text = "Hello, world! My name is Kai!" console.log(text.startsWith("Hello")); // true console.log(text.endsWith("world")); // false
10. How to Replace All Occurrences of a String
There are multiple ways for replacing all occurrences of a string. You can either use the
String#replace method and a regular expression with the global flag. Or, you could use the new
String#replaceAll method. Note that this new method is not yet available in every browser and Node.js version.
const text = "I like apples. You like apples." console.log(text.replace(/apples/g, "bananas")); // "I like bananas. You like bananas." console.log(text.replaceAll("apples", "bananas")); // "I like bananas. You like bananas."
Thanks a lot for reading this post. Please consider sharing it with your friends and colleagues. See you soon!