In this chapter, we will explore the lastIndexOf()
method in TypeScript. This method is a built-in function that helps in finding the index of the last occurrence of a specified substring within a string. Understanding how to use lastIndexOf()
is useful for locating substrings and their positions within strings, especially when you need to find the last occurrence.
Table of Contents
- Definition
- Syntax
- Examples
- Conclusion
1. Definition
The lastIndexOf()
method returns the index of the last occurrence of a specified substring within a string. If the substring is not found, it returns -1
.
2. Syntax
string.lastIndexOf(searchString, position?);
Parameters
searchString
: The substring to search for within the string.position
(optional): The position within the string at which to begin searching backwards forsearchString
. Defaults to the length of the string.
Return Value
The method returns a number representing the index of the last 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 lastIndexOf()
works in TypeScript.
Example 1: Basic Usage
In this example, we find the index of the last occurrence of a specific substring within a string.
let str: string = "Hello, TypeScript! TypeScript is fun!";
console.log(str.lastIndexOf("TypeScript")); // Output: 19
console.log(str.lastIndexOf("JavaScript")); // Output: -1
Example 2: Using lastIndexOf()
with Position
In this example, we use the position
parameter to start the search from a specific index.
let str: string = "Hello, TypeScript! TypeScript is fun!";
console.log(str.lastIndexOf("TypeScript", 18)); // Output: 7
console.log(str.lastIndexOf("TypeScript", 8)); // Output: 7
console.log(str.lastIndexOf("Hello", 5)); // Output: 0
Example 3: Case Sensitivity
The lastIndexOf()
method is case-sensitive. In this example, we see how case sensitivity affects the result.
let str: string = "Hello, TypeScript! TypeScript is fun!";
console.log(str.lastIndexOf("typescript")); // Output: -1
console.log(str.lastIndexOf("TypeScript")); // Output: 19
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.lastIndexOf("")); // Output: 18 (length of the string)
console.log(str.lastIndexOf("", 5)); // Output: 5
Example 5: Finding Multiple Occurrences
In this example, we find the index of the last occurrence of a substring within a string containing multiple occurrences of that substring.
let str: string = "TypeScript is great. TypeScript is fun. TypeScript is powerful.";
let firstOccurrence = str.indexOf("TypeScript");
let lastOccurrence = str.lastIndexOf("TypeScript");
console.log(firstOccurrence); // Output: 0
console.log(lastOccurrence); // Output: 36
4. Conclusion
In this chapter, we explored the lastIndexOf()
method in TypeScript, which is used to find the index of the last 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 lastIndexOf()
effectively can help in various string manipulation tasks in TypeScript, especially when locating the last occurrence of substrings within strings.
Comments
Post a Comment
Leave Comment