Delete part of string by index position in javascript

This article will discuss removing part of the string either from the start of the string or from the end based on an index position. There will be illustrations using different examples.

Table of Contents:-

Javascript string remove from start till the index

  1. Using substring() method

Example:-

Remove part of string “Javascript is popular language” till index position 10

Code:-

let dummyString = 'Javascript is popular language'
dummyString = dummyString.substring(0,10) // note that indexes start from zero.
console.log( dummyString )

Explanation:-

Here, we use javascript’s substring() method to get a portion of the original string from the beginning until the specified index position. The indexes are zero-based. The character at the end-index gets excluded in the returned string.

Output:-

Javascript

2. Using slice() method

Example:-

Remove part of string “Javascript is popular language” till index position 10

Code:-

let dummyString = 'Javascript is popular language'
dummyString = dummyString.slice(0,10) // note that indexes start from zero.
console.log( dummyString )

Explanation:-

Here, similar to substring(), we use javascript’s slice() method to get a portion of the original string from the beginning until the specified index position. The indexes are zero-based. The character at the end-index gets excluded in the returned string.

Output:-

Javascript

Javascript string remove from index to end

  1. Using substring() method

Example:-

Remove part of string “Javascript is popular language” from index position 10 till end

Code:-

let dummyString = 'Javascript is popular language'
dummyString = dummyString.substring(10)  // note that indexes start from zero.
console.log( dummyString )

Explanation:-

Since the indexes are zero-based, substring (10) means starting from the 10th index position till the end of the string. If the end-index is not specified, it means to extract the string till the end. Note that character at start-index is included in the returned string. Hence the space gets included in the starting.

Output:-

 is popular language

2. Using slice() method

Example:-

Remove part of string “Javascript is popular language” till index position 10

Code:-

let dummyString = 'Javascript is popular language'
dummyString = dummyString.slice(10) // note that indexes start from zero.
console.log( dummyString )

Explanation:-

Since the indexes are zero-based, slice(10) means starting from the 10th index position till the end of the string. If the end-index is not specified, it means to extract the string till the end. Note that character at start-index is included in the returned string. Hence the space gets included in the starting.

Output:-

 is popular language

Read More:

I hope this article helped you to delete a part of the javascript string based on indexes. Good Luck !!!

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top