When working with strings in TypeScript projects, I frequently need to extract a portion of text between specific characters. In this typescript tutorial, we will see how to get string between 2 characters in typescript using different methods.
For example, the string is, Mindvalley-Afest#2023, the first character is – and the second character is #, so the between string is ‘Afest’.
These are the methods to get a string between 2 characters in TypeScript:
- Using a regular expression
- Using split() method
- Using slice() method
Get The String Between 2 Characters In TypeScript Using The Split() Method
Here we will see how to get a string between 2 characters in TypeScript using the split() method.
The split method in typescript is used to divide a string into an array of substrings, based on a given separator.
Syntax:
str.split(separator: string | RegExp, limit?: number)
Let’s see an example to get a string between 2 characters in TypeScript using the split method.
For example, we will use the first method using split(), which will split the string into an array, at the startCharVal and endCharVal. And then fetch the second element of the resulting array.
Open the code editor, and create a file ‘stringBetween2Character’ , write the below code:
const strVal = 'Mindvalley-Afest#2023';
const startCharVal = "-";
const endCharVal = "#";
const startIndex = strVal.indexOf(startCharVal) + 1;
const endIndex = strVal.indexOf(endCharVal, startIndex);
const result = strVal.substring(startIndex, endIndex);
console.log(result);
To compile the code and run the command below, you can see the result in the console.
ts-node stringBetween2Character.ts

This is how to get a string between 2 characters in TypeScript using the split() method.
Check out Convert String To Double In TypeScript
Get The String Between 2 Characters In TypeScript Using A Regular Expression
Here we will see how to get a string between 2 characters in TypeScript using a regular expression.
For example, we will split the string on the occurrence of the “-” and “#” using the split method, then it will provide an array of strings. And by using the index [1], we will get the second string from an array.
In the ‘stringBetween2Character’ file write the below code:
const strVal = 'Mindvalley-Afest#2023';
const result = strVal.split(/[-#]/);
console.log(result); // 👉️ [ 'bobby', 'hadz', 'com' ]
console.log(result[1]);
To compile the code and run the below command and you can see the result in the console.
ts-node stringBetween2Character.ts

This is how to get a string between 2 characters in TypeScript using a regular expression.
Check out Get String After Character In TypeScript
Get The String Between 2 Characters In TypeScript Using The Slice() Method
Here we will see how to get a string between 2 characters in TypeScript using the slice() method.
The slice() method in typescript will extract a portion of an array, and with the extracted item it will return a new array. This method accepts two arguments i.e starting index and ending index.
Syntax:
array.slice(startIndex, endIndex)
Let’s see an example of how to get a string between 2 characters in TypeScript using the slice() method.
For example, we will use the indexOf(), and the slice method is used to get the substring between two characters in the string.
In the ‘stringBetween2Character’ file write the below code:
const strVal = 'Mindvalley-Afest#2023';
const startCharVal = "-";
const endCharVal = "#";
const startIndex = strVal.indexOf(startCharVal) + 1;
const endIndex = strVal.indexOf(endCharVal, startIndex);
const result = strVal.slice(startIndex, endIndex);
To compile the code and run the command below, you can see the result in the console.
ts-node stringBetween2Character.ts

This is an example of how to get a string between 2 characters in TypeScript using the slice method.
Conclusion
In this TypeScript tutorial, we saw how to get a string between 2 characters in TypeScript using a different method. So, the different methods are:
- Using a regular expression
- Using split() method
- Using slice() method
You may also like the following typescript tutorials:
- TypeScript Check If String Is Number
- TypeScript keyof with Strings
- Multiline Strings in TypeScript
- Compare Strings in TypeScript

Hey! I’m Bijay Kumar, founder of SPGuides.com and a Microsoft Business Applications MVP (Power Automate, Power Apps). I launched this site in 2020 because I truly enjoy working with SharePoint, Power Platform, and SharePoint Framework (SPFx), and wanted to share that passion through step-by-step tutorials, guides, and training videos. My mission is to help you learn these technologies so you can utilize SharePoint, enhance productivity, and potentially build business solutions along the way.