Typescript check if a string contains a substring

In this typescript tutorial, we will see how to check if a string contains a substring in typescript.

To check the string contains the substring, by using the different methods in typescript:

  • include()
  • indexOf()
  • search()

Use include() to check if a string contains a substring in typescript

Before we start checking if a string contains a substring in typescript, we need to see what is include() method.

The include() in typescript is used to check if a string contains a substring or not.

Syntax of include() in typescript

array.includes(item, index)

In the above syntax,

  • the item is the item to search in the array
  • The index is the starting index for the search. It is a boolean value. It might be positive or negative. The length of the array is appended to the start index for negative values.

Returns:

  • It only returns a boolean value. If the element is found in the array, true; otherwise, false.
  • It returns false if the start index is larger than or equal to the length of the array.

Let’s see how we can implement the include(), to check if the string contains a substring.

Example 1: We will take a string and we will find a substring from that string, if it is available then it returns true or else false.

Open the code editor, create a file named ‘containsSubstring.ts’, and then write the below code:

let str= 'hey! i am typescript. Learn me';
let substr= 'hey'

const result= str.includes(substr);
console.log(result);

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

ts-node containsSubstring
Using include() check if the string contains the substring in the typescript
Using include() check if the string contains the substring in the typescript

Example 2: As the typescript include() method is case sensitive. We will do the case insensitive check if a string contains a substring, for this convert the string to lowercase.

See also  How To Split String And Get Last Element In Typescript?

In the containsSubstring.ts, write the below code:

let str= 'Hey! i am typescript. Learn me';
 let substr= 'hey'
 const result= str.toLowerCase().includes(substr.toLowerCase())
 console.log(result);

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

ts-node containsSubstring
check if the string contains the substring in the typescript
check if the string contains the substring in the typescript

These are examples of checking if a string contains substrings in typescript using the include() method.

Use indexOf() to check if a string contains a substring in typescript

Here we will see what is indexOf(), then we will see an example of how to use indexof() method to check if a string contains a substring in typescript.

The indexOf() method in typescript starts the search at fromIndex and returns -1 if the supplied value cannot be found. It returns the index of the initial occurrence of the defined value within the calling String object.

Syntax of

string.indexOf(searchValue[, fromIndex]) 

In this syntax,

  • search value: this is the value searched for.
  • fromIndex: This is a location that specifies the starting point of the search within the calling text.

Let’s see an example to check if the string contains a substring using indexOf () in typescript.

Example 1: we will define a string, by calling the indexOf() on a string and passing the substring as a parameter. If it found the substring from the string then it will return the starting index of the substring within a string.

In the containsSubstring.ts, write the below code:

let str= 'Hey! i am typescript. Learn me';
 let substr= 'Learn'
 const result= str.indexOf(substr);
 console.log(result);

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

ts-node containsSubstring
check if the string contains the substring in the typescript
check if the string contains the substring in the typescript

Example 2: As the indexOf is case-sensitive. If you need to verify if a string does not include a substring in a case-insensitive way, change both strings to lowercase.

See also  unique Keyword in TypeScript

In the containsSubstring.ts, write the below code:

function isnotIncludes(string:any, substring:any) {
    return string.toLowerCase().indexOf(substring.toLowerCase())=== -1;
  }

  let str= 'Hey! i am typescript. Learn me';
  console.log(isnotIncludes(str,'Typescript'))//👉 false( because it includes typescript, so it is not equal to -1)
  console.log(isnotIncludes(str,'Javascript'))//👉true(not included in string, it equal to -1)

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

ts-node containsSubstring
how to check if the string contains the substring in the typescript using indexOf
how to check if the string contains the substring in the typescript using indexOf

This is how we can check the substring is available in a string using the indexOf method in typescript.

Use search() to check if a string contains a substring in typescript

Here we will check if a string contains a substring in typescript using search() method.

The search() method in TypeScript is used to find a match between a regular expression and this String object.

Syntax of search()

string.search(regexp);

In this syntax

regexp: This is a RegExp object

Return:

This function returns the regex index within the string. Else, -1 is returned.

Let’s see how we can check if the string contains a substring using the search method in the typescript.

Example 1: we will take a string, and then we will use the search method, to check whether the substring is there in the string or not. if the substring is there, then it will return starting index of the substring within a string.

In the containsSubstring.ts, write the below code:

let str= 'hey! i am typescript. Learn me';
let substr= 'Learn'

const result= str.search(substr);
console.log(result);

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

ts-node containsSubstring
how to check if the string contains the substring in the typescript using search
how to check if the string contains the substring in the typescript using the search

Example 2: As the search method is case-sensitive. If you need to verify if a string does not include a substring in a case-insensitive way, change both strings to lowercase. Below you can see how we can do it.

See also  super Keyword in Typescript

In the containsSubstring.ts, write the below code:

let str= 'hey! i am typescript. Learn me';
let substr= 'Typescript'

const result= str.toLowerCase().search(substr.toLowerCase());
console.log(result);

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

ts-node containsSubstring
check if the string contains the substring in the typescript using search
check if the string contains the substring in the typescript using the search

This is how we can check if the string contains the substring in the typescript using the search method.

Conclusion

In this typescript tutorial, we saw different methods to check whether a substring is available in a string in typescript. We covered these methods with different examples.

  • include()
  • indexOf()
  • search()

You may like the following typescript tutorials:

>