Often we come across a requirement to remove the first N characters from a string in javascript. This article will discuss removing N characters from the starting of the string based on the value we pass for N using many examples illustrations.

Table of Contents:-

Javascript remove first N characters from a string using substring()

Javascript’s substring(sIndex, eIndex) returns a substring from the original string based on the start and end indexes passed as parameters.

  • The sIndex specifies from where the extracted substring should begin. The character at the sIndex is included in the substring and is the first argument of the method.
  • The eIndex specifies till which character the extracted substring will be formed, excluding the character at the eIndex. This eIndex is the second argument of the string and is optional. If it is not present while calling the method, the substring() method should extract until the end of the string.

Syntax:-

<original_string>.substring(N)

Example1:-

Remove the first 3 characters from the string “123Javascript”

Code:-

let dummyString = "123Javascript"
let finalString = dummyString.substring(3)
console.log("original string: " + dummyString)
console.log("final string: " + finalString)

Explanation:-

The above code uses the substring() method to retrieve a part of the original string, starting from the Nth position till the end of the string. The index is based on 0. The value of N, in this case, is 3.

Output:-

original string: 123Javascript
final string: Javascript 

Example2:-

Remove the first 5 characters from the string “Hello Javascript”

Code:-

let dummyString = "Hello Javascript"
let finalString = dummyString.substring(5)
console.log("original string: " + dummyString)
console.log("final string: " + finalString)

The value of N, in this case, is 5.

Output:-

original string: Hello Javascript
final string:  Javascript

Javascript remove first N characters from a string using slice()

Javascript’s slice(sIndex, eIndex) returns a new string object part of the original string but does not modify it.

  • The sIndex specifies from where the extraction should begin and is the first argument of the method. The index is zero-based, and if this value is negative, it means -> length of the original string + startIndex
  • The eIndex specifies that the extraction will end just before the eIndex and is the second argument of the method. The eIndex is optional. If it is not present while calling the method, the slice() method should extract until the end of the string.

Syntax:-

<original_string>.slice(N)

Example1:-

Remove the first 3 characters from the string “123Javascript”

Code:-

let dummyString = "123Javascript"
let finalString = dummyString.slice(3)
console.log("original string: " + dummyString)
console.log("final string: " + finalString)

Explanation:-

The above code uses the slice() method to get a portion from the original starting, which starts from the character at Nth index till the end of the string. The value of N is 3.

Output:-

original string: 123Javascript
final string: Javascript

Example2:-

Remove the first 5 characters from the string “Hello Javascript”

Code:-

let dummyString = "Hello Javascript"
let finalString = dummyString.slice(5)
console.log("original string: " + dummyString)
console.log("final string: " + finalString)

The value of N, in this case, is 5.

Output:-

original string: Hello Javascript
final string:  Javascript

Read More:

We hope this article helped you to delete the first N characters from a string in javascript. Good Luck !!!