;

How to Remove the First Character From a String in Javascript


Tutorialsrack 30/05/2021 Jquery Javascript

In this article, you’ll learn how to remove the first character from a string in Javascript. You can achieve this using a slice(), substring() and substr() methods.

How to Remove the Last Word from the String Using JavaScript

How to Remove Last Character From String Using Javascript or JQuery

How to Remove the First and Last Character from a String using Javascript

Example 1: Remove First Character From a String Using Slice Method

In this example, we used the slice() method of javascript. The slice() method slices the array from the start index to the end index. The end index if unspecified is taken as array length. 

Here is an example to remove the first character from the string using the slice() method in Javascript.

Example 1: Remove First Character From a String Using Slice Method
let input = "Tutorialsrack.com"

// Function to Remove Character
function removeCharacter(str){
  let tmp = str.split('');
  return tmp.slice(1).join('');
}

let output = removeCharacter(input);
console.log(`Output is ${output}`);
// "Output is utorialsrack.com"

Example 1: Remove First Character From a String Using substring() Method

In this example, we used the substring() method of javascript. The substring() method returns a string between the start index and the end index. The end index if unspecified is assumed as string length, as in this case.

Here is an example to remove the first character from the string using the substring() method in Javascript.

Example 1: Remove First Character From a String Using substring() Method
let input = "Tutorialsrack.com"

// Function to Remove Character
function removeCharacter(str){
  return str.substring(1)
}

let output = removeCharacter(input);
console.log(`Output is ${output}`);
// "Output is utorialsrack.com"

Example 3: Remove First Character From a String Using substr() Method

In this example, we used the substr() method of javascript. The substr() method returns the specified number of characters from the specified index from the given string. And the second parameter length is optional. The length is used for the number of characters to extract. If omitted, it extracts the rest of the string. If the length is 0 or negative value then it returns an empty string.

Here is an example to remove the first character from the string using the substr() method in Javascript.

Example 3: Remove First Character From a String Using substr() Method
let input = "Tutorialsrack.com"

// Function to Remove Character
function removeCharacter(str){
  return str.substr(1)
}

let output = removeCharacter(input);
console.log(`Output is ${output}`);
// "Output is utorialsrack.com"

I hope this article will help you to understand how to remove the first character from a string in Javascript.

Share your valuable feedback, please post your comment at the bottom of this article. Thank you!


Related Posts



Comments

Recent Posts
Tags