Skip to content
the poor coder

Substring in Javascript using substring

Poor Man
Published
Reading time
2 min read
On this page

Substring in Javascript using substring#

As a web developer, I have come across many instances where I needed to extract a part of a string in Javascript. This is where the substring method comes in handy. It allows us to extract a specified portion of a string and return it as a new string. The basic syntax of the substring method is as follows:

Text

str.substring(startIndex, endIndex);

The startIndex parameter is the starting index of the substring we want to extract. The first character in the string is at index 0. The endIndex parameter is optional and specifies the end index of the substring we want to extract. If this parameter is not specified, the substring will include all characters from the startIndex to the end of the string.

Example 1#

Let's say we have the following string:

Text

const str = "Hello world";

If we wanted to extract the word "world" from this string, we would use the following code:

Text

const word = str.substring(6);
console.log(word); // "world"

In this example, we specified a starting index of 6, which is the index of the letter "w" in the word "world". Since we did not specify an end index, the substring includes all characters from index 6 to the end of the string.

Example 2#

Let's say we have the following string:

Text

const str = "Hello world";

If we wanted to extract the word "Hello" from this string, we would use the following code:

Text

const word = str.substring(0, 5);
console.log(word); // "Hello"

In this example, we specified a starting index of 0, which is the index of the first letter in the string. We also specified an end index of 5, which is the index of the letter "o" in the word "Hello". The substring includes all characters from index 0 to index 4 (the character at index 5 is not included).

It's worth noting that the substring method does not modify the original string. Instead, it returns a new string that contains the specified portion of the original string.

Alternative Methods#

There are a few alternative methods that can also be used to extract substrings in Javascript:

  • substr: This method is similar to substring, but it uses a different syntax. The first parameter is still the starting index, but the second parameter specifies the length of the substring. If the second parameter is not specified, the substring will include all characters from the starting index to the end of the string.
  • slice: This method works similarly to substring and substr, but it allows for negative indices. Negative indices count from the end of the string instead of the beginning. If a negative index is used as the starting index, it counts from the end of the string. If a negative index is used as the end index, it counts from the beginning of the string.

Overall, the substring method is a simple and effective way to extract substrings in Javascript. However, it's always good to be aware of alternative methods that may better suit your specific use case.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC