There are different methods of splitting a string in Javascript. Splitting a string comes in handy at many levels and occasions while coding. There are many ways to split for whatever results you want. However, in this article we are only going to discuss one or two methods for each way. In this article we are going to have a look at different ways to split a string to yield different results. So, let’s get into it.
Split a string into substring
In Javascript one can approach splitting a string into substrings via many different and creative ways. One of the easiest and recommended ways is using the split() method. Here is how:
There are two parts to the split() method. One is the separator and the other is the limiter. Separator part is important, because without it the method will return the string as it is. Separator can either be a string to identify a pattern for where to split the original string, or it can be a regular expression. On the other hand, a limiter is to limit the number of strings you get as a result of split.
Example:
let str = “Hello world this is a string”;
let substring = str.split(' '); // here separator is a space
console.log(substring); // [“Hello”, “world”, “this”, “is”, “a”, “string”]
let str = “Hello-world-this-is-a-string”;
let substring = str.split(' '); // here separator is “-”
console.log(substring); // [“Hello”, “world”, “this”, “is”, “a”, “string”]
let substring = str.split(' ',2); // here limiter is 2
console.log(substring); // [“Hello”, “world”]
let str = “Hello world! This is a-String?”;
let substr = str.split(/[-,?,!,]/); // using Regular Expression: RegEx
console.log(substr); // [“Hello world”, “This is a”, “String”, “”]
There are many other methods to split a string into substrings. For instance, the substring() method or using a custom code using a for loop. But, this method is efficient and it is better to use this.
Split a string into N sized sub strings
Following we have mentioned two different ways to split a string into many different substrings of the size you want. For instance you want to split each letter of the string.
1st way
Using the split method but with different parameters and with a filter() method. The split() takes a number to define what size splits you want.
var str = "1234567890".split(/(.{2})/).filter(O=>O)
console.log(str) //[ '12', '34', '56', '78', '90' ]
2nd way
Using the match() method you get the same results and it works in the same way. The difference is that you just have to replace the “n” with the number of string splits you want.
var str = "abcdefghijklmnopqrstuvwxyz".match(/.{1,n}/g);
console.log(str)
Split a string into array
Splitting a string into an array is similar to splitting a string into substrings. You can use any method that works for splitting string into substrings. However, when assigning or pushing the splitted elements to somewhere just make sure it is an array variable. For instance, have a look at the following code.
let str = “Hello world this is a string”;
let arr = str.split(' '); // here separator is a space
console.log(arr); // [“Hello”, “world”, “this”, “is”, “a”, “string”]
Split a string into characters
Splitting a string into characters is also similar to splitting into substrings. The only difference is that you split every character. Check out the following code.
let str = “Hello world this is a string”;
let arr = str.split(' '); // here separator is a space
console.log(arr);
// Output:
[
'H', 'e', 'l', 'l', 'o', ' ',
'w', 'o', 'r', 'l', 'd', ' ',
't', 'h', 'i', 's', ' ', 'i',
's', ' ', 'a', ' ', 's', 't',
'r', 'i', 'n', 'g'
]
There are many ways to split strings and get the results you desire. There is just a slight difference in the method and that is it. Now you know, so prepare better and be ready to ace your Javascript interviews. If you are searching to hire JS developers then this is a resource for your interview. Javascript developers for hire available at vteams are pre-vetted for your convenience. Check out vteams today and hire javascript programmers you desire.
No comments yet