Open In App

Delete First Character of a String in TypeScript

Last Updated : 12 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Deleting the first character of a string in Typescript consists of removing the character at the 0th index of the string. Using various inbuilt methods, we can extract the first character of the input string and print or return only the remaining characters apart from the first character.

There are many ways to delete the first character of the string in typescript which are as follows:

Using slice() method

The slice() method extracts the part of the input string and returns the extracted part in the new string. If we remove the string’s first character, it can be done by mentioning the start index from which we want to extract the string.

Syntax:

string.slice(startingindex, endingindex);

Example: In the below example, we have passed the start index as 1 in slice(1). This extracts the string except the first character.

Javascript




let input: string = "GeeksforGeeks";
let output: string = input.slice(1);
console.log(output);


Output:

eeksforGeeks

Using substring() method

The substring() method returns the part of the given string from the start index to the end index provided as the argument.

Syntax:

str.substring(startIndex, endIndex);

Example: To demonstrate extracting the string except the first character from the string which we passed as the start index as 1 in the substring(1) method.

Javascript




let input: string = "GeeksforGeeks";
let output: string = input.substring(1);
console.log(output);


Output:

eeksforGeeks

Using array destructuring

The array destructuring allows us to mainly extract the values from the arrays and assign them to the variables. Then, we can join the characters into the new string.

Syntax:

let [variable1, variable2, ..., variableN] = array;

Example: In the below example, the [,…rest] uses the array destructuring to skip the first character of the input string, and then using the join method for joining the remaining characters into a new string.

Javascript




let input: string = "GeeksforGeeks";
let [, ...rest] = input;
let output: string = rest.join('');
console.log(output);


Output:

eeksforGeeks

Using replace() method

The replace() method can be used to replace the matched character with an empty string, this can be done by giving the regular expression pattern as the argument to the method.

Syntax:

 str.replace(/^./, '');

Example: To demonstrate removing the first character in the input string and print the rest of the string character using the regular expression.

Javascript




let input: string = "GeeksforGeeks";
let output: string = input.replace(/^./, '');
console.log(output);


Output:

eeksforGeeks


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads