Typescript split string by dot [Using 3 Methods]

In this typescript tutorial, we will see how to spit string by dot in typescript using different typescript methods. We will see a few examples of typescript split string by dot.

There are 3 different methods to split string by dot in typescript, these are

  1. The split() method
  2. The substring() and indexOf() methods
  3. The RegExp object

The split() method

Here we will see how we can use the split() method to split the string by dot in typescript.

The split() method in typescript is used for dividing text into an array of substrings. It accepts as an input a separator string and returns an array of substrings separated by the separator string. Here’s an example of how to split a string by dot using the split() method.

Open the code editor, create a file ‘splitStringByDot.ts’, then write the below code:

const myString = "hello.world";
const splitString = myString.split(".");
console.log(splitString); // 👉Output: ["hello", "world"]

To compile the code run the below command and you can see the result in the console:

ts-node splitStringByDot
Typescript split string by dot using split()
Typescript split string by dot using split()

This is how we can split string by dot using split() method in typescript.

The substring() and indexOf() methods

Here we will see how we can use the substring() method and indexOf() to split the string by dot in typescript.

To divide a text by a dot, use the substring() and indexOf() methods combined. The indexOf() function in typescript, returns the position of the first occurrence of a specified substring. In contrast, the substring() method returns a substring of the original string depending on the start and end positions.

Here’s an example of how to divide a text by dot using the substring() and indexOf() methods in typescript.

In the splitStringByDot.ts file write the below code

const myString = "hello.world";
const dotPosition = myString.indexOf(".");
const firstPart = myString.substring(0, dotPosition);
const secondPart = myString.substring(dotPosition + 1);
console.log(firstPart); // Output: "hello"
console.log(secondPart); // Output: "world"

To compile the code run the below command and you can see the result in the console:

ts-node splitStringByDot
Typescript split string by dot using substring() and indexOf()
Typescript split string by dot using substring() and indexOf()

This is how we can split string by dot using the substring() and indexOf() methods in typescript.

The RegExp object

Here we will see how we can use the RegExp object to split the string by dot in typescript.

The RegExp object may be used to build a regular expression pattern that matches the separator string, and then the split() function can be used to split the string with that pattern. Here’s an example of how to divide a string by a dot using the RegExp object:

In the splitStringByDot.ts file write the below code:

const myStr = "hello.world";
const splitString = myStr.split(/\./);
console.log(splitString); // Output: ["hello", "world"]

To compile the code run the below command and you can see the result in the console:

ts-node splitStringByDot
Typescript split string by dot using regExp
Typescript split string by dot using regExp

This is how we can split string by dot using regExp Object in typescript.

Conclusion

In this typescript tutorial, we saw the different methods to split strings by dot in typescript. Here are the 3 methods we covered:

  1. The split() method
  2. The substring() and indexOf() methods
  3. The RegExp object

You may like the following typescript tutorials:

>