TypeScript String slice()

In this chapter, we will explore the slice() method in TypeScript. This method is a built-in function that helps in extracting a section of a string and returning it as a new string. Understanding how to use slice() is useful for manipulating and extracting specific parts of strings.

Table of Contents

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

1. Definition

The slice() method extracts a section of a string and returns it as a new string, without modifying the original string. It takes two arguments: the starting index and the ending index (optional).

2. Syntax

string.slice(beginIndex, endIndex?);

Parameters

  • beginIndex: The zero-based index at which to begin extraction. If negative, it is treated as str.length + beginIndex.
  • endIndex (optional): The zero-based index before which to end extraction. The character at this index will not be included. If omitted, extraction continues to the end of the string. If negative, it is treated as str.length + endIndex.

Return Value

The method returns a new string containing the extracted section of the original string.

3. Examples

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

Example 1: Basic Usage

In this example, we extract a section of a string using positive indexes.

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

Example 2: Omitting the End Index

In this example, we extract a section 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.slice(7);
console.log(result); // Output: TypeScript!

Example 3: Using Negative Indexes

In this example, we use negative indexes to extract a section of a string.

let str: string = "Hello, TypeScript!";
let result = str.slice(-10, -1);
console.log(result); // Output: TypeScript

Example 4: Extracting the Entire String

In this example, we extract the entire string using the slice() method.

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

Example 5: Extracting with Out of Range Indexes

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

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

4. Conclusion

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