| by Arround The Web | No comments

How to Split String by Space Character in JavaScript

In programming, splitting the string into substrings based on the specified delimiter is a common task. The use of a delimiter depends on the user’s requirements. If no delimiter is provided, the original string is returned as the standard output. The user can use any delimiter like a number, letter, or character to split the string.

This guide elaborates on all the possible methods to split the string by a “space character” in JavaScript.

How to Split String by Space Character in JavaScript?

JavaScript offers the “split()” method to split the given string into an array format separated by the comma, space character, or any letter specified as a “delimiter”. This method splits the string according to the specified separator passed as its first argument.

Syntax

string.split(separator, limit)

 
In the above-generalized syntax:

    • string: It represents the initialized string that needs to be split.
    • separator: It is the first parameter that specifies the special character based on which the string needs to be split.
    • limit: It is the second additional parameter that defines the limit of the string at which the user wants to split the string.

Let’s use the above-discussed method to split the initialized string by space character.

Example 1: Applying the “split()” Method to Split String by Space Character

In this example, the “split()” method is applied to split the initialized string by space character.

JavaScript Code

Follow the given code:

<script>
let string = 'Welcome to Linuxhint!';
let result = string.split(' ');
console.log(result);
</script>

 
In the above lines of code:

    • Firstly, the stated string value is initialized.
    • Next, the “result” variable applies the “split()” method having the “space character” as a separator (as method argument).
    • Lastly, the “console.log()” method displays the “result” variable output on the console.

Output


As seen, the initialized array is split based on the “space” character and converted into an array with “3” indexes.

Example 2: Applying the “split()” Method to Split String by Space Character Using “Whitespace RegExp(\s)”

In the second example, the “whitespace regular expression(\s)” is used with the “split()” method to split the initialized string by space character:

<script>
let string = 'Welcome to Linuxhint!';
let result = string.split(/\s/);
console.log(result);
</script>

 
Here, the “split()” method uses the whitespace regexp “\s” as its argument to split the defined string by the space character.

Output


As analyzed, the output is the same as “Example 1” i.e., a given string is split by a space character successfully.

Conclusion

To split the string by space character, use the JavaScript predefined “split()” method by passing the “space character” or the “whitespace regexp(\s)” as its argument. Both these arguments split the initialized string based on the “space” character and transform it into an array format. Apart from the space character, this method can also split the string by a specified letter, number, or special character. This guide briefly explained the complete procedure to split the string by space character.

Share Button

Source: linuxhint.com

Leave a Reply