Examples of String split() Method
1. Basic Usage:
Example: The below Simple example illustrates the String split() method in TypeScript.
JavaScript
<script> // Original strings var str = "w3wiki - Best Platform" ; // use of String split() Method var newarr = str.split( " " ); console.log(newarr); </script> |
Output:
[ 'w3wiki', '-', 'Best', 'Platform' ]
In this case, we split the string using a space as the separator.
2. Specifying a Limit:
Example: You can also limit the number of splits. For instance:
JavaScript
<script> // Original strings var str = "w3wiki - Best Platform" ; // use of String split() Method var newarr = str.split( "" ,14); console.log(newarr); </script> |
Output:
[ 'G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'g', 'e', 'e', 'k', 's', ' ' ]
Here, we limit the splits to 14 characters.
TypeScript String split() Method
The split()
method in TypeScript allows you to split a string into an array of substrings by separating the string using a specified separator. It’s a method for handling string manipulation. In this article, we’ll learn the details of this method, and provide practical examples.
Syntax of String split() Method:
string.split([separator][, limit])
Parameter: This method accepts two parameters as mentioned above and described below:
- separator – The character used for separating the string. It can be a regular expression or a simple string.
limit
(optional): An integer specifying the maximum number of splits to be found.
Return Value: This method returns the new array.