quedemy-logo
substring-in-javascript

Javascript substring() Method

April 15, 2023 · 5 min read

substring() in javascript select a part of the string and return a substring. In this article, we will learn to work with strings using the substring() method.

You will learn what is substring and learn to extract a substring from a string by applying different methods. We will discuss substring and several ways to extract a substring from a string

Table of Contents

What is Substring in JavaScript?

  • Consider a string Hello Everyone. In the string Hello Everyone, The string Hello is a substring of Hello Everyone. Also, the string Everyone is a substring of the string Hello Everyone.
  • Therefore, a small part of a string can be called a substring. The length of a substring can vary from 1 character to the entire length of the original string

Note: Remember that only the part of a continuous sequence of characters can be considered a substring

  • For example, The substrings of the string quedemy could be:
'quedemy',
'que', 'demy',
'qu', 'ue', 'ed', 'em', 'my',
'q', 'u', 'e', 'd', 'e', 'm', 'y'

Get Substring in Javascript

We can use the following methods to extract substring in javascript: substring() method and slice() method. Let's discuss in details.

The substring() Method

  • We can use built-in Javascript substring() method to get substrings from a given string.
  • The syntax for the substring() method is: substring(indexStart, indexEnd)
  • substring() method can accept two arguments as indexStart and indexEnd. Then, the substring() method returns a substring from the starting index to the ending index.
  • Returned result from substring() method includes character of start index and does not include character of last index.
const exStr = 'E-learning'

// 1. Get substring from startIndex '0' to endIndex '6'
console.log(exStr.substring(0, 7)) // output: E-learn

// 2. Get substring from startIndex '3' to endIndex '6'
console.log(exStr.substring(3, 7)) // output: earn
  • In the above code, we have a string E-learning. We applied the substring() method to get the substring E-learn and earn.
  • endIndex is optional parameter. If the value of endIndex is not specified, it extracts all characters till the end of the string
const exStr = 'E-learning'

// get substring from startIndex '3' till the end
console.log(exStr.substring(3)) // output: earning

Note:

  • indexStart will be treated as 0, if value of indexStart is undefined, NaN, or not specified. Therefore, the substring() method returns the whole string.
const exStr = 'E-learning'
console.log(exStr.substring(undefined)) // output: E-learning
console.log(exStr.substring(NaN)) // output: E-learning
console.log(exStr.substring()) // output: E-learning
  • If value of indexStart is equal to the value of indexEnd, substring() method will return an empty string.
const exStr = 'E-learning'
console.log(exStr.substring(2, 2)) // output: ""

The slice() Method

  • slice() is also a built-in method in javascript and returns a part of the string
  • The syntax for the slice() method is: slice(indexStart, indexEnd)
const str = 'Crack Interviews'

// Return a substring from index 0 to index 4
console.log(str.slice(0, 5)) // output: Crack

// Return a substring from index 2 to index 3
console.log(str.slice(2, 4)) // output: ac
quedemy-logo

© 2024 Quedemy. All rights reserved

LinkedIn

Company

About usBlogContact usSitemap