TypeScript String substring()

In this chapter, we will explore the substring() method in TypeScript. This method is a built-in function that helps in extracting a part of a string between two specified indices. Understanding how to use substring() is useful for manipulating and extracting specific sections of strings.

Table of Contents

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

1. Definition

The substring() method returns a new string containing the characters of the original string from (and including) the start index to (but not including) the end index. If no end index is provided, the method extracts characters to the end of the string.

2. Syntax

string.substring(startIndex, endIndex?);

Parameters

  • startIndex: The zero-based index at which to begin extraction. If negative or greater than the length of the string, it is treated as 0.
  • endIndex (optional): The zero-based index before which to end extraction. If omitted, extraction continues to the end of the string. If greater than the length of the string, it is treated as the length of the string. If less than startIndex, the two indices are swapped.

Return Value

The method returns a new string containing the specified part of the given string.

3. Examples

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

Example 1: Basic Usage

In this example, we extract a part of a string using valid start and end indices.

let str: string = "Hello, TypeScript!";
let result = str.substring(7, 17);
console.log(result); // Output: TypeScript

Example 2: Omitting the End Index

In this example, we extract a part of a string from a starting index to the end of the string by omitting the end index.

let str: string = "Hello, TypeScript!";
let result = str.substring(7);
console.log(result); // Output: TypeScript!

Example 3: Swapping Indices

If the endIndex is less than startIndex, substring() swaps the two indices.

let str: string = "Hello, TypeScript!";
let result = str.substring(17, 7);
console.log(result); // Output: TypeScript

Example 4: Using Indices Out of Range

In this example, we use indices that are out of the range of the string length.

let str: string = "Hello, TypeScript!";
let result1 = str.substring(7, 50);
console.log(result1); // Output: TypeScript!
let result2 = str.substring(-5, 5);
console.log(result2); // Output: Hello

Example 5: Extracting the Entire String

In this example, we extract the entire string using the substring() method by providing 0 and the string's length as indices.

let str: string = "Hello, TypeScript!";
let result = str.substring(0, str.length);
console.log(result); // Output: Hello, TypeScript!

4. Conclusion

In this chapter, we explored the substring() method in TypeScript, which is used to extract a part of a string between two specified indices. We covered its definition, syntax, parameters, return value, and provided several examples to demonstrate its usage. Understanding how to use substring() effectively can help in various string manipulation tasks in TypeScript, especially when extracting specific sections of 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