Loading...

Split string in JavaScript

Split string in JavaScript

What’s a string in Javascript?

In any programming language, a string is a sequence of characters. And in JavaScript, it is no different. Strings here are enclosed in single quotes or double quotes or backquotes. And also, they are immutable. A negligible tweak in an existing string results in a new string. The old existing string is not changed and its memory is claimed implicitly. Further, strings in JavaScript are highly case-sensitive. The same word in lower and upper cases will be considered as two different strings. Eg: “javascript” and “Javascript” are two different strings.  Here is how we can declare a string in JavaScript –

let string1 = “Javascript split”
let string2 = new String(“Javascript split”)

There are many methods and properties used with strings in javascript. One of the string methods is split()

What is the split() string method?

As the name suggests, the split() method divides a given string into an array of substrings. And finally returns this array of substrings. And the original array remains unchanged. Let us understand the syntax of this method.

Syntax:

let string = "test string";
string.split(separator, limit)

Let us understand each argument in the syntax.

Separator

This argument is optional. We can use the split string method even without putting a separator in the syntax. In that case, the array that is returned contains only one element that consists of the entire string. We usually put the pattern that describes where the split should occur in the string. When we put multiple characters in this argument, the entire exact sequence must be found in the string to split the string at those places.

Limit

This argument is an integer and specifies the maximum number of substrings allowed in the array. The split method stops splitting as soon as either the string is parsed completely to the limit is reached. If the limit is reached before the string is parsed completely, the rest of the string is not considered at all. When the limit value is zero, an empty array is returned. This argument is also optional like that of the separator argument.

Return value

A copy of the supplied string is split ( based on the arguments passed ) into an array. This array of strings is returned by this method.
Let us understand it with the help of some examples:

Splitting a string by each character:

let info = 'I learn coding from codedamn'
console.log(info.split(''))

Output:

[ "I", " ", "l", "e", "a", "r", "n", " ", "c", "o", "d", "i", "n", "g", " ", "f", "r", "o", "m", " ", "c", "o", "d", "e", "d", "a", "m", "n"]

Now let us split the string based on space character and return an array containing the substrings.

let info = 'I learn coding from codedamn'
let arr = info.split(' ');
console.log(arr);

Output:

[“I”, “learn”, “coding”, “from”, “codedamn”]

We can access each element of the array by using the array methods.

Split with a limit:

We have already discussed the limit argument in the syntax. Now let us understand how it works with the help of an example

let info = 'I learn coding from codedamn'
console.log(message.split(' ', 3))

Output:

[“I”, “learn”, “coding”]

If we change the limit to zero, then the returned value would be an empty array.

let info = 'I learn coding from codedamn'
info.split(' ',0)

Output:

[]

Reversing a string:

We can reverse a string using the split() method. We do it by using a chain that contains split(), reverse(), and join() methods. Let us see it with the help of an example.

const str = 'codedamn';
str.split('').reverse().join('');

Output:

“nmadedoc”

The split() method splits the string into an array
The reverse() method reverses the whole array
The join() method joins back the reversed array and forms a string

Conclusion

The split() method divides a string into a list of substrings and puts these substrings in an array and returns that array. This is all about the split() string method in JavaSript. This method is very useful when we need to extract specific elements from the array in our code. If you want to learn javascript from scratch or level up to advance javascript, feel free to check out other articles and courses on Codedamn. Codedamn offers the best platform to learn and practice code. It has an in-browser coding playground which can be accessed from any device. Also, join our discord community such that you don’t miss out on any new programs or updates.

Sharing is caring

Did you like what Agam singh, Aman Ahmed Siddiqui, Aman Chopra, Aman, Amol Shelke, Anas Khan, Anirudh Panda, Ankur Balwada, Anshul Soni, Arif Shaikh, wrote? Thank them for their work by sharing it on social media.

0/10000

No comments so far