How to get string after character in Typescript

In this typescript tutorial, we will see how to get string after character in typescript using different methods.

For example, the string is “fest-2020”, then the character is ‘-‘, and after that, the string is 2020.

Here are the 5 methods to get string after character in Typescript that we are going to cover:

  • Using split()
  • Using subsrting() and indexOf()
  • Using substring() and lastIndexOf()
  • Using replace()
  • Using slice ()

Get string after character in Typescript using split()

Here we will see how we can use the split method to get the string after a character in the typescript.

The split method in typescript is used to divide a string object into an array of strings by separating the string into substrings.

Let’s see an example of how to split the method to get the string after a character in typescript.

For example, we will define the value “Country-USA” in the variable “myStringVal”, we will apply the split method to the variable. Which split the ‘Country-USA” into an array of substrings by using the separator variable as a delimiter.

Then, using the [1] index, the second element of the resulting array is accessed, which includes the string after the separator. As we know the starting index of the array is 0, so it starts from 1.

Open the code editor, and create a typescript project file named ‘stringAfterCharacters.ts’. Write the below code:

const myStringVal = "Country-USA";
const separator = "-";

const result = myStringVal.split(separator)[1];
console.log(result); // Output: USA

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

ts-node stringAfterCharacter.ts
Typescript get string after character using split method
Typescript get string after character using the split method

This is how we can get string after character using the split method in Typescript.

See also  get Keyword in Typescript

Get string after character in Typescript using substring() and indexOf()

Here we will see how to get string after character using substring() and indexOf() in Typescript.

In TypeScript, using the substring() method, we can take a string and extract a specific part of it to create a new string. It requires the starting index and the ending index of the substring to be extracted as two arguments.

Syntax:

str.substring(startIndex, endIndex)

The indexOf method in typescript returns the first index of a given item is found in an array, else -1, if not item is found

Syntax:

str.indexOf(searchValue[, fromIndex])

Let’s see an example, to get string after character using substring() and indexOf() in Typescript.

For example, we define a string and the separator in a variable, as you can see below. Then we use the indexOf() to get the index of the separator character, from the defined string. After that, substring() is used to extract that part of the string, which starts after the separator.

In the ‘stringAfterCharacters.ts’, file write the below code:

const myStringVal = "Country-USA";
const separator = "-";

const result = myStringVal.substring(myStringVal.indexOf(separator) + 1);
console.log(result); // Output:USA

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

ts-node stringAfterCharacter.ts
Typescript get string after character using substring method and indexOF()
Typescript get string after character using substring method and indexOF()

This is how we can get string after character using substring method and indexOF() in Typescript.

Get string after character in Typescript using substring() and lastIndexOf()

Here we will see how to get string after character using substring() and lastIndexOf() in Typescript.

In TypeScript, using the substring() method, we can take a string and extract a specific part of it to create a new string. It requires the starting index and the ending index of the substring to be extracted as two arguments.

See also  Typescript check if a string contains a substring

Syntax:

str.substring(startIndex, endIndex)

When a string is searched for a certain substring, TypeScript’s lastIndexOf() method returns the index of the last instance of the substring.

It requires one mandatory parameter, the substring to be searched, and one optional additional parameter, the index at which the search should begin.

Syntax:

str.lastIndexOf(searchValue, fromIndex)

Let’s see an example, to get string after character using substring() and lastIndexOf() in Typescript.

For example, we define a string and the separator in a variable, as you can see below. Then we use the lastIndexOf() which is used in the below code to search the index of the last occurrence of the separator character in the string. After that, substring() is used to extract that part of the string, which starts after the separator.

In the ‘stringAfterCharacters.ts’, file write the below code:

const myStringVal = "Country-USA";
const separator = "-"; 
const result = myStringVal.substring(myStringVal.lastIndexOf(separator) + 1);
console.log(result); // Output:USA

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

ts-node stringAfterCharacter.ts
Typescript get string after character using substring method and lastIndexOF()
Typescript get string after character using substring method and lastIndexOF()

This is an example to get string after character using the substring method and lastIndexOf() in of Typescript.

Read How to map an array of objects to another array of objects in Typescript

Get string after character in Typescript using replace()

Here we will see how to get string after character using replace() in Typescript.

The replace method in typescript is used to replace old ones, some and all matches of a string or RegExp into a new string.

Syntax:

replace(pattern, replacement)

Now let’s see an example, to get string after character using replace() in Typescript.

For example, we define a string and separator, so we will replace everything before the separator by using replace method. If the regular expression, ‘/^.*?\-/’, matches any character ‘.*?’, from the starting of the string(‘^’) up to the first occurrence of the hyphen character ‘\-/’.

See also  TypeScript Array filter() Method

In the ‘stringAfterCharacters.ts’, file write the below code:

const myStringVal = "Country-USA";
const separator = "-"; 

const result = myStringVal.replace(/^.*?\-/, "");
console.log(result); // Output: USA

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

ts-node stringAfterCharacter.ts
Typescript get string after character using replace
Typescript get string after character using replace()

This is an example of Typescript get string after character using replace().

Get string after character in Typescript using slice()

Here we will see how to get string after character using slice() in Typescript.

The slice method in typescript is used to remove a part of an array and returns a new array.

Syntax:

array.slice( begin [,end] );

Let’s see an example to get string after character using slice() in Typescript.

For example, we will use the indexOf method to get the index of the separator character, in the mystringVal variable. After that, we will use the slice method, to extract the section of that string that starts after the separator.

In the ‘stringAfterCharacters.ts’, file write the below code:

const myStringVal = "Country-USA";
const separator = "-"; 
const result = myStringVal.slice(myStringVal.indexOf(separator) + 1);
console.log(result); // Output:USA

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

ts-node stringAfterCharacter.ts
get string after character using slice() in Typescript
get string after character using slice() in Typescript

This is how to get string after character using slice() in Typescript.

Conclusion

In this typescript tutorial, we saw how to get a string after a character in typescript by using different methods. So the methods we covered:

  • Using split()
  • Using substring() and indexOf()
  • Using substring() and lastIndexOf()
  • Using replace()
  • Using slice ()

You may also like:

>