Typescript split string multiple separators

Do you want to split string with multiple separators in Typescript? In this Typescript tutorial, I will explain how to split string with multiple separators in Typescript with various methods.

To split strings in TypeScript with multiple separators, like separating by commas, semicolons, and spaces, you can use regular expressions. For instance, cityString.split(/[,; ]+/) will split a string like “New York, Los Angeles; Chicago Houston” into an array of individual city names. This method is efficient and concise, making it ideal for handling strings with varied separators.

Split string with multiple separators in Typescript

Here, we will see various methods to split strings in TypeScript using multiple separators.

Method 1: Using Regular Expressions

The most versatile method for splitting strings with multiple separators in TypeScript is by using regular expressions. This method allows you to define a pattern that includes multiple separators.

Example:

Let’s say we have a string of city names separated by commas, semicolons, and spaces. We use a regular expression that matches commas, semicolons, and spaces to split this string into individual city names. Here is the complete code.

let cityString: string = "New York, Los Angeles; Chicago Houston";
let separators: RegExp = /[,; ]+/;
let cities: string[] = cityString.split(separators);
console.log(cities);

Output:

[ 'New', 'York', 'Los', 'Angeles', 'Chicago', 'Houston' ]

You can see the output in the screenshot below after I executed the code using Visual Studio code:

Typescript split string multiple separators

Also, read Typescript Split String

See also  static Keyword in Typescript

Method 2: Chaining split() and concat()

If you prefer not using regular expressions, another approach is to chain split() and concat() methods. Here is a complete code:

Example:

let cityString: string = "New York, Los Angeles; Chicago Houston";
let cities: string[] = cityString.split(',')
    .map(part => part.split(';'))
    .reduce((acc, val) => acc.concat(val), [])
    .map(part => part.trim().split(' '))
    .reduce((acc, val) => acc.concat(val), []);
console.log(cities);

We can split the string multiple times and concatenate the results; you can see the output in the screenshot below:

typescript split multiple separators

Method 3: Using replace() and split()

Another method involves replacing all separators with a common separator and then using split() method in Typescript.

Example:

let cityString: string = "New York, Los Angeles; Chicago Houston";
let normalizedString: string = cityString.replace(/(;| )+/g, ',');
let cities: string[] = normalizedString.split(',');
console.log(cities);

Here, it replace all semicolons and spaces with commas, then splits:

Method 4: Custom Split Function

For more complex scenarios, you might want to create a custom split function to split string multiple separators in Typescript.

Example:

function customSplit(input: string, separators: string[]): string[] {
  return input.split(new RegExp(separators.join('|'), 'g'));
}

let cityString: string = "New York, Los Angeles; Chicago Houston";
let cities: string[] = customSplit(cityString, [',', ';', ' ']);
console.log(cities);

Conclusion

In TypeScript, splitting strings with multiple separators can be achieved in various ways, each suitable for different scenarios. Regular expressions offer the most flexibility while chaining split() and concat() or using replace() provides simpler, albeit less powerful, alternatives.

In this Typescript tutorial, I have explained how to split string multiple separators in Typescript using various methods with examples.

You may also like:

>