Break a string into two equal-size substrings in JavaScript
This post will discuss how to break a string into two equal-size substrings in JavaScript.
To break a string into two equal-size substrings in JavaScript, we can use one of the following functions:
1. Using slice() function
We can use the slice() function of the string object, which returns a section of the string between two indices. We can get the length of the string using the length property, and divide it by two to get the midpoint. Then, we can use the slice() function to get the first and second halves of the string from the start to the midpoint, and from the midpoint to the end. For example, this code splits a string into two equal-length substrings using this function.
|
1 2 3 4 5 6 7 8 |
let str = "abcdefghi"; let half = Math.ceil(str.length / 2); // get the half point let firstHalf = str.slice(0, half); // get the first half let secondHalf = str.slice(half); // get the second half console.log(firstHalf); // "abcde" console.log(secondHalf); // "fghi" |
2. Using substring() function
Alternatively, we can use the split() function of the string object in the similar way as above, which return a portion of the string between the start and the end index. For example, to split a string into two equal-size substrings, we can use the following code:
|
1 2 3 4 5 6 7 8 |
let str = "abcdefghi"; let half = Math.ceil(str.length / 2); // get the half point let firstHalf = str.substring(0, half); // get the first half let secondHalf = str.substring(half); // get the second half console.log(firstHalf); // "abcde" console.log(secondHalf); // "fghi" |
3. Using match() function
Another way to break a string into two equal-size substrings in JavaScript is to use a regular expression. The match() function searches a string for a pattern and returns an array of matches. We can use it with a regular expression that matches any character (.) exactly half of the length of the string times ({n}). We can use parentheses () to capture the matched substrings. Then, we can use the match() function on the string to return an array of matches that match the regular expression. Here’s an example:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
let str = "abcdefghi"; // get the half point let half = Math.ceil(str.length / 2); // Create a regular expression that matches two substrings of half length let regex = new RegExp(`(.{${half}})(.{${str.length - half}})`); // Get an array of the matched substrings // substrings is ["abcdefghi", "abcde", "fghi"] let substrings = str.match(regex); // Get the first and the second substring from the array let firstHalf = substrings[1]; let secondHalf = substrings[2]; console.log(firstHalf); // "abcde" console.log(secondHalf); // "fghi" |
Here’s another regular expression to match two equal-size substrings in a string. This regular expression matches any substring of length between 1 and half, and finds all such substrings in the string.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
let str = "abcdefghi"; // get the half point let half = Math.ceil(str.length / 2); // The regular expression to match let regex = new RegExp(`.{1,${half}}`, "g"); // The array of matches let substrings = str.match(regex); // Get the first and the second substring from the array let firstHalf = substrings[0]; let secondHalf = substrings[1]; console.log(firstHalf); // "abcde" console.log(secondHalf); // "fghi" |
The code creates a regular expression object using the new RegExp() constructor, which takes a string pattern and optional flags as arguments. The string pattern is .{1,${half}}, which means any character (.) repeated at least once and at most half times ({1,half}). The flag "g" means global, which means find all matches in the string, not just the first one. Then, we can use the match() function to get an array of the captured substrings.
That’s all about breaking a string into two equal-size substrings in JavaScript.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)