How to get string between 2 characters in Typescript

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
get a string between 2 characters in a typescript
get a string between 2 characters in a typescript using split methods

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
get a string between 2 characters in a typescript
get a string between 2 characters in a typescript using a regular expression

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
get a string between 2 characters in a typescript using slice() method
get a string between 2 characters in a typescript using the slice method

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:

>