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
- The
split()
method - The
substring()
andindexOf()
methods - 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

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

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

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:
- The
split()
method - The
substring()
andindexOf()
methods - The
RegExp
object
You may like the following typescript tutorials:
- How to create a multiline string in typescript
- How to split a string with multiple separators in TypeScript?
- Typescript check if a string contains a substring
- How to remove quotes from a string in Typescript
- Typescript string union vs enum
- Typescript convert string to keyof
I am Bijay a Microsoft MVP (8 times –Â My MVP Profile) in SharePoint and have more than 15 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