Do you want to know how to split a string in Typescript? In this Typescript tutorial, I will explain everything about Typescript split string. Especially how to split strings in Typescript using various methods.
To split a string in TypeScript, you can use the split()
method, which divides the string into an array of substrates based on a specified separator. For more complex patterns, regular expressions can be used as separators. Additionally, you can limit the number of splits by providing a second argument to split()
.
Split string in Typescript
Let us check various methods to split strings in Typescript.
- Using the split() Method
The split() method in Typescript is the most common way to divide a string into an array of substrates based on a specified separator. Here is an example of a Typescript string split with complete code:
let text: string = "TypeScript is amazing!";
let words: string[] = text.split(" ");
console.log(words);
In this example, the string is split at each space, resulting in an array of words. You can see the output in the screenshot below:
Output:
[ 'TypeScript', 'is', 'amazing!' ]
When using split(), it’s important to consider edge cases. For instance, splitting an empty string returns an array with one empty string element, not an empty array. Additionally, handling cases where the separator is not found in the string is crucial to avoid unexpected results.
let text: string = "";
let result: string[] = text.split(",");
console.log(result); // [""]
text = "TypeScript";
result = text.split(",");
console.log(result);
You can see the output in the screenshot below:
- Split string with a Limit
You can also limit the number of splits by providing a second argument to split() function in Typescript. Here is a complete code for split a string in Typescript.
let text: string = "TypeScript is amazing and versatile!";
let words: string[] = text.split(" ", 3);
console.log(words);
Here, the string is split into only the first three words. Check out the screenshot below for the output.
- Split a Typescript string with a Regular Expression
You also use the regular expression to split in Typescript. Here is a complete code to split a string in Typescript using regular expression.
let text: string = "TypeScript, JavaScript, Python";
let languages: string[] = text.split(/,\s*/);
console.log(languages);
This splits the string at each comma, regardless of the number of spaces following it. here is the output below; check the screenshot.
[ 'TypeScript', 'JavaScript', 'Python' ]
- Splitting and Mapping
Combining split() with map() allows for more sophisticated operations on each substring. Here is a complete code for the typescript split string.
let text: string = "1 SharePoint, 2 PowerApps, 3 Power Automate";
let numbers: number[] = text.split(", ").map(s => parseInt(s.split(" ")[0]));
console.log(numbers);
This extracts the number from each “number language” pair in the string. Here is the output you can check in the screenshot below:
- Splitting with Destructuring
Destructuring can be used to capture specific parts of the split string into variables in Typescript.
let text: string = "SharePoint:PowerApps:PowerBI";
let [first, second, third] = text.split(":");
console.log(first, second, third);
This assigns each language to a separate variable. You can see the output in the screenshot below:
Typescript split string by comma
Now, let us take another example of Typescript split string. I will show you how to split a string by comma in Typescript.
To split a string by a comma in TypeScript, you can use the split()
method with a comma (,
) as the separator. Here’s an example:
let csvData: string = "SharePoint, PowerApps, PowerBI";
let technology: string[] = csvData.split(", ");
console.log(technology);
Output:
[ 'SharePoint', 'PowerApps', 'PowerBI' ]
You can see the screenshot below:
Typescript split string by character
To split a string by a specific character in TypeScript, you again use the split()
method, specifying the character as the separator. Here’s an example where we split a string by the character -
:
Here is the complete code to split a string by character in Typescript.
let dateString: string = "2025-11-30";
let dateParts: string[] = dateString.split("-");
console.log(dateParts);
Output:
[ '2025', '11', '30' ]
Check out the screenshot below for the output also:
In this example, the string dateString
representing a date is split at each hyphen (-
), resulting in an array containing the year, month, and day as separate strings.
Typescript split string by slash
Here is another Typescript split string example. In this case, we will see how to split a string by slash in Typescript.
To split a string by a slash (/
) in TypeScript, you can use the split()
method with /
as the separator. Here’s an example:
let filePath: string = "user/documents/photos/image.jpg";
let pathParts: string[] = filePath.split("/");
console.log(pathParts);
In this example, the string filePath
representing a file path is split at each slash (/
), resulting in an array containing the different parts of the path.
Typescript split string by semicolon
In Typescript, you can easily split a string by semicolon with a few lines of code. Here’s an example of splitting a string by a semicolon (;
) in TypeScript:
let settings: string = "color:blue; size:medium; style:casual";
let options: string[] = settings.split("; ");
console.log(options);
In this example, the settings
string, which contains key-value pairs separated by semicolons, is split into an array of individual settings. Each element in the options
array represents one setting, like "color:blue"
.
Conclusion
TypeScript offers a powerful approach to string manipulation, particularly when it comes to splitting strings. Whether you need to split a string by spaces, commas, characters, slashes, or semicolons, TypeScript’s split() method will be useful for you. Here, we explained how to use the split() method in Typescript to split a string with real examples. We discussed the below Typescript split string examples:
- Typescript split string by comma
- Typescript split string by character
- Typescript split string by slash
- Typescript split string by whitespace
- Typescript split string by semicolon
You may also like:
- Typescript split string by dot
- Typescript check if a string contains a substring
- How to create a multiline string in typescript
- Typescript split string multiple separators
- How to Split String by Length in Typescript?
- How to Split String By New Line in Typescript?
I am Bijay a Microsoft MVP (10 times – My MVP Profile) in SharePoint and have more than 17 years of expertise in SharePoint Online Office 365, SharePoint subscription edition, and SharePoint 2019/2016/2013. Currently working in my own venture TSInfo Technologies a SharePoint development, consulting, and training company. I also run the popular SharePoint website EnjoySharePoint.com