22 C
Jaipur
Tuesday, November 24, 2020

Javascript split – Linux Hint

Must read

How to Install Gibbon LMS on Ubuntu 20.04

Gibbon is a free and open-source school management system specially designed for teachers, students, parents and leaders. It helps teachers to find, contact and...

Linux GPU Monitoring and Diagnostic Commands Line Tools

A video card is a special circuit board that controls what is displayed on a computer monitor. It is also called a graphics processing...

Javascript is a scripting or programming language of the web. Strings are an important part of the variables in any programming language. We often need to manipulate string according to our needs. In this article, we will talk about the javascript’s split string method which is used to split the string according to our needs. So, let’s take a look at what is a string and what does split string method do.

The string is just a simple text or characters which can include the alphabets, numbers, or symbols.

Javascript’s split() method is called upon when it is required to split the string into the array of substrings in accordance to the separator that you provide.

Syntax

Let’s have a look at the syntax of the split method.

string.split([seperator][, limit]);

Here, the separator could be a single character using which you would like to split the string.

And the limit is the limit of splits. When the number of substrings becomes equal to the limit, the split() method stops.

Let’s dive in and get a couple of examples done for the understanding of the split() function.

Examples

We suppose a string “Linuxhint is great and working hard to grow.”. Now, let’s try to split the string into arrays of string using the split() method. By providing the “ ” space character as a separator. But, without providing the limit. Later, we will do it with the limit.

As you can see that the split() method has returned the array of substrings, based on the “ ” space.

Now, let’s see what happened if we provide a limit as well.

As you can see that the split() function stopped splitting the string, and it would stop splitting the string when the count is equivalent to the number of substrings.

Now, let’s see if we don’t provide a separator just an empty string, and calls the split() function.

As you can see that the function has split and returned the array of each character separated.

Now, let’s see if we don’t provide both of the arguments and just calls the split() function.

Great, the split() function has returned an array with a single substring in it that is the whole string

Pro tip

What if we want to split the string on the base of two separators or we want separators as well in the output array of substring? Luckily, there is a solution as well, We can provide regular expressions as a separator as well.

So, let’s see how can we split the string with two separators. Space “ “ character and “i” character

Alright! It worked great. As we expect it to split.

Now, what if we want to include the separators as well in the array of the substring. We will simply add parenthesis() around the regular expression.

Perfect, as you can see that the separators are also included in the array of substrings.

Conclusion

In this article, we have seen that how can we split a string on the base of a separator provided and how can we apply the limit to the split() function. We came across how we could provide regular expressions in compliance with our needs and requirements. So, have a happy and the best learning of javascript with linuxhint.com.

Source link

- Advertisement -

More articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest article

How to Install Gibbon LMS on Ubuntu 20.04

Gibbon is a free and open-source school management system specially designed for teachers, students, parents and leaders. It helps teachers to find, contact and...

Linux GPU Monitoring and Diagnostic Commands Line Tools

A video card is a special circuit board that controls what is displayed on a computer monitor. It is also called a graphics processing...