codewithjohn.dev
Published on

Understanding the JavaScript substring() Function

In this article you are going to learn how to use the JavaScript substring() method to extract a substring from a string.

Sometimes in Our program, we may need to get one or more substrings from a complete sentence or paragraph. In that case, we can use the substring() method.

To understand the substring() method, we must first understand its basic syntax and functionality. The substring() method returns part of a string, meaning it returns a specified part of the string between start and end indexes. The substring method Returns a new string containing the specified part of the given string.

substring() Syntax

str.substring(startIndex)
str.substring(startIndex, endIndex)

substring() Parameters

The substring() method takes two parameters:

  • startIndex - specifies the index of the first character to include in the returned substring.

  • endIndex - determines the first character to exclude from the returned substring. meaning that the returned substring doesn’t include the character at the endIndex. If omitted, it extracts until the end of the string.

If startIndex equals endIndex, then the substring() method returns an empty string. And if startIndex is greater than the endIndex, then the substring() swaps their roles: the startIndex becomes the endIndex and vice versa.

Any NaN argument value is treated as 0.

If either startIndex or endIndex is less than zero substring() considers it as zero (0) and if either startIndex or endIndex is greater than the string.length the substring() considers it as str.length.

Here are some examples

// Example 1: Extracting a portion of a URL
let url = 'https://www.example.com/products'
let domain = url.substring(8)
console.log(domain) // Output: www.example.com/products

// Example 2: Extracting the file extension from a filename
let filename = 'document.txt'
let extension = filename.substring(filename.lastIndexOf('.') + 1)
console.log(extension) // Output: txt

// Example 3: Extracting the first name from a full name
let fullName = 'John Doe'
let firstName = fullName.substring(0, fullName.indexOf(' '))
console.log(firstName) // Output: John

// Example 4: Extracting the last name from a full name
let fullName = 'John Doe'
let lastName = fullName.substring(fullName.lastIndexOf(' ') + 1)
console.log(lastName) // Output: Doe

// Example 5: Extracting a substring with negative indices
let str = 'Hello World!'
let substring = str.substring(-5, -1)
console.log(substring) // Output: Hello

// Example 6: Extracting a substring with dynamic parameters
let str = 'Hello World!'
let start = 1
let end = 8
let substring = str.substring(start, end)
console.log(substring) // Output: ello Wo

// Example 7: Extracting a substring from a multiline string
let multilineString = `Hello
World!`
let substring = multilineString.substring(0, multilineString.indexOf('\n'))
console.log(substring) // Output: Hello

// Example 8: Extracting a substring from a string with special characters
let str = 'Hello World!'
let substring = str.substring(0, str.indexOf('!') + 1)
console.log(substring) // Output: Hello World!

// Example 9: Extracting a substring from a string with leading and trailing spaces
let str = '   Hello World!   '
let substring = str.substring(str.trim().indexOf('H'))
console.log(substring) // Output: Hello World!

// Example 10: Extracting a substring with non-integer parameters
let str = 'Hello World!'
let start = 1.5
let end = 7.8
let substring = str.substring(Math.floor(start), Math.ceil(end))
console.log(substring) // Output: ello W