check if a string
contains another string
[ indexOf ]

Check if a string contains another string

Write a function that takes two strings and returns true if a string contains a given substring, otherwise returns false.


Example ...

Enter a string and a substring ...

"Many moons ago" original string

"moo" substring

The function will return true if string contains given substring.

The function will return false if string does not contain given substring.

Strings are a sequence of zero or more characters written inside quotes used to represent text.

Strings may consist of letters, numbers, symbols, words, or sentences.

Strings are immutable, they cannot be changed.

Each character in a string has an index.

The first character will be index 0 the second character will be index 1 and so on.

There are two ways to access an individual character in a string.

charAt() method

const str1 = "abc"; string

str1.charAt(0); character at index 0 → "a"

str1.charAt(1); character at index 1 → "b"

str1.charAt(2); character at index 2 → "c"

str1.charAt(3); character at index 3 → "" index not found

Alternatively use at() or slice() methods

bracket notation []

const str2 = "abc"; string

str2[0]; character at index 0 → "a"

str2[1]; character at index 1 → "b"

str2[2]; character at index 2 → "c"

str2[3]; character at index 3 → undefined index not found


Check if a string contains another string using indexOf() method.


indexOf() method returns the position of the first occurrence of specified value(s) in a string, or -1 if not found.

It takes an optional starting position and returns the first occurrence of the specified substring at an index greater than or equal to the specified number.

syntax

indexOf(searchString)

indexOf(searchString, position)

Find first index of substring "o"

const str3 = "Hello World";

str3.indexOf("o"); returns ↴

4 → index of first occurrence of substring "o"

Substring found.

Find first index of substring "o" starting from index 6

const str4 = "Hello World";

str4.indexOf("o", 6); returns ↴

7 → index of first occurrence of substring "o" from index 6

Substring found.

Find first index of substring "a"

const str5 = "Hello World";

str5.indexOf("a"); returns ↴

-1 → substring "a" NOT found

When checking if a specific substring occurs within a string, the correct way to check is test whether the return value is -1

const str6 = "Hello World";

str6.indexOf("o") !== -1; returns ↴

true → substring "o" found.

const str7 = "Hello World";

str7.indexOf("a") !== -1; returns ↴

false → substring "a" NOT found.


Initialize a variable to hold the string to check if it contains another string.

const string1 = "hello big world"; → user input

Initialize a variable to hold the substring to check if it is within the string.

const string1 = "hello big world"; → user input


Define a function containsSubstring to check if a string contains another string

function containsSubstring(str, substring) {}

The function takes two strings as input str and substring and returns true if substring found in string, otherwise returns false.

indexOf checks if a substring exists within a string.

return str.indexOf(substring) !== -1

If indexOf is NOT -1 then the substring has been found in the string, return true.

If indexOf is -1 then the substring has NOT been found in the string, return false.

If the function returns true, substring found in the string

If the function returns false, substring not found in the string


Call the function with ↴

containsSubstring(string1, string2);


Check if a string contains another string.

const string1 = "hello big world";

const string2 = "big";

function containsSubstring(str, substring) {

return str.indexOf(substring) !== -1;

}

call function

containsSubstring(string1, string2); returns ↴

true

Check if a string contains another string