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 string between 2 characters in Typescript:
- Using regular expression
- Using split() method
- Using slice() method
Get string between 2 characters in Typescript using split() method
Here we will see how we can get a string between 2 characters in typescript using split() methods.
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 split methods.
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 below command and you can see the result in the console.
ts-node stringBetween2Character.ts
This is how we can get a string between 2 characters in typescript using split() methods.
Get string between 2 characters in Typescript using regular expression
Here we will see how we can get a string between 2 characters in typescript using 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 we can get a string between 2 characters in typescript using regular expression.
Get string between 2 characters in Typescript using slice() method
Here we will see how we can 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 we can 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 below command and 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 we can get a string between 2 characters in typescript using a different method. So, the different methods are:
- Using regular expression
- Using split() method
- Using slice() method
You may also like the following typescript tutorials:
- How to Get Key by Value from enum String in Typescript
- Typescript string enum reverse mapping
- How to get string after character in Typescript
- convert string to double in typescript
- How to convert a string to boolean 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