TypeScript String indexOf()

In this chapter, we will explore the indexOf() method in TypeScript. This method is a built-in function that helps in finding the index of the first occurrence of a specified substring within a string. Understanding how to use indexOf() is useful for locating substrings and their positions within strings.

Table of Contents

  1. Definition
  2. Syntax
  3. Examples
  4. Conclusion

1. Definition

The indexOf() method returns the index of the first occurrence of a specified substring within a string. If the substring is not found, it returns -1.

2. Syntax

string.indexOf(searchString, position?);

Parameters

  • searchString: The substring to search for within the string.
  • position (optional): The position within the string at which to begin searching for searchString. Defaults to 0.

Return Value

The method returns a number representing the index of the first occurrence of the searchString within the string. If the substring is not found, it returns -1.

3. Examples

Let's look at some examples to understand how indexOf() works in TypeScript.

Example 1: Basic Usage

In this example, we find the index of a specific substring within a string.

let str: string = "Hello, TypeScript!";
console.log(str.indexOf("TypeScript")); // Output: 7
console.log(str.indexOf("JavaScript")); // Output: -1

Example 2: Using indexOf() with Position

In this example, we use the position parameter to start the search from a specific index.

let str: string = "Hello, TypeScript!";
console.log(str.indexOf("Type", 5)); // Output: 7
console.log(str.indexOf("Hello", 1)); // Output: -1

Example 3: Case Sensitivity

The indexOf() method is case-sensitive. In this example, we see how case sensitivity affects the result.

let str: string = "Hello, TypeScript!";
console.log(str.indexOf("typescript")); // Output: -1
console.log(str.indexOf("TypeScript")); // Output: 7

Example 4: Searching for an Empty String

In this example, we search for an empty string within another string.

let str: string = "Hello, TypeScript!";
console.log(str.indexOf("")); // Output: 0
console.log(str.indexOf("", 5)); // Output: 5

Example 5: Finding Multiple Occurrences

In this example, we find the index of the first and subsequent occurrences of a substring within a string.

let str: string = "TypeScript is great. TypeScript is fun.";
let firstOccurrence = str.indexOf("TypeScript");
let secondOccurrence = str.indexOf("TypeScript", firstOccurrence + 1);
console.log(firstOccurrence); // Output: 0
console.log(secondOccurrence); // Output: 21

4. Conclusion

In this chapter, we explored the indexOf() method in TypeScript, which is used to find the index of the first occurrence of a specified substring within a string. We covered its definition, syntax, parameters, return value, and provided several examples to demonstrate its usage. Understanding how to use indexOf() effectively can help in various string manipulation tasks in TypeScript, especially when locating substrings and their positions within strings.

Comments

Spring Boot 3 Paid Course Published for Free
on my Java Guides YouTube Channel

Subscribe to my YouTube Channel (165K+ subscribers):
Java Guides Channel

Top 10 My Udemy Courses with Huge Discount:
Udemy Courses - Ramesh Fadatare