Site icon codippa

3 different methods for substring in javascript

Substring means a portion of a string. Substring is extracted based on numeric indexes. This article will explain 3 different ways in which a substring can be retrieved from a string in javascript.
Method 1: substr function
Javascript substr function takes two arguments with their meanings given below.
1. Start index: Index of character from which the substring will be extracted.
2. Count: Total number of characters or length of substring.
Syntax of substr function is

substr(index, length)

By default characters in a string start at index 0 from left to right. Below illustration explains the working of substr function in javascript.

Following points should be remembered regarding substr function.

Examples of substr function based on above pointers are given below.

   let str = "Technology";
   document.write("String:: " + str)
   document.write('<br>------------------------<br>');
   document.write("str.substr() => " + str.substr());
   document.write('<br>');
   document.write("str.substr(1, 5) => " + str.substr(1, 5));
   document.write('<br>');
   document.write("str.substr(NaN, 4) => " + str.substr(NaN, 4));
   document.write('<br>');
   document.write("str.substr(-3, 2) => " + str.substr(-3, 2));

Above example will print below output on browser.

String:: Technology
————————
str.substr() => Technology
str.substr(1, 5) => echno
str.substr(NaN, 4) => Tech
str.substr(-3, 2) => og

Method 2: substring function
Javascript substring function accepts 2 numeric arguments. First is the start index and second is the end index and returns the string between the start and (end index – 1).
Working of substring can be understood with following illustration.
Following points are worth considering for substring function.

Go through following examples for a better understanding of substring function.

  let str = "Technology";
  document.write("String:: " + str)
  document.write('<br>------------------------<br>');
  document.write("str.substring() => " + str.substring());
  document.write('<br>');
  document.write("str.substring(1, 5) => " + str.substring(1, 5));
  document.write('<br>');
  document.write("str.substring(NaN, 4) => " + str.substring(NaN, 4));
  document.write('<br>');
  document.write("str.substring(4, NaN) => " + str.substring(4, NaN));
  document.write('<br>');
  document.write("str.substring(-3, 2) => " + str.substring(-3, 2));
  document.write('<br>');
  document.write("str.substring(3, 0) => " + str.substring(3, 0));

This code will print below output on browser window.

str.substring() => Technology
str.substring(1, 5) => echn
str.substring(NaN, 4) => Tech
str.substring(4, NaN) => Tech
str.substring(-3, 2) => Te
str.substring(3, 0) => Tec

Method 3: slice function
Javascript slice function is also used to extract a substring from a string. It accepts two numeric arguments where first argument is the start index and second argument is the end index and returns the string between those two indexes excluding the character at end index.
Following points should be remembered related to slice function.

Go through below examples carefully to understand the usage of slice function.

   let str = "Technology";
   document.write("String:: " + str)
   document.write('<br>------------------------<br>');
   document.write("str.slice() => " + str.slice());
   document.write('<br>');
   document.write("str.slice(1, 5) => " + str.slice(1, 5));
   document.write('<br>');
   document.write("str.slice(1, 20) => " + str.slice(1, 20));
   document.write('<br>');
   document.write("str.slice(NaN, 4) => " + str.slice(NaN, 4));
   document.write('<br>');
   document.write("str.slice(4, NaN) => " + str.slice(4, NaN));

Above code will output

String:: Technology
————————
str.slice() => Technology
str.slice(1, 5) => echn
str.slice(1, 20) => echnology
str.slice(NaN, 4) => Tech
str.slice(4, NaN) =>

Negative indexes in slices
As stated earlier, character index in a string start from 0. If the string is traversed from right to left, then the indexes are counted negative with the first character at the end having index -1, second as -2 and so on as shown below.

slice supports negative indexes as per the following rules.

Below examples will be helpful in understanding how slice handles negative arguments.

   let str = "Technology";
   document.write("String:: " + str)
   document.write('<br>------------------------<br>');
   document.write("str.slice(-3) => " + str.slice(-3));
   document.write('<br>');
   document.write("str.slice(2, -3) => " + str.slice(2, -3));
   document.write('<br>');
   document.write("str.slice(-3, 9) => " + str.slice(-3, 9));
   document.write('<br>');
   document.write("str.slice(-3, 2) => " + str.slice(-3, 2));
   document.write('<br>');
   document.write("str.slice(-3, -1) => " + str.slice(-3, -1));

Above code will output

String:: Technology
————————
str.slice(-3) => ogy
str.slice(2, -3) => chnol
str.slice(-3, 9) => og
str.slice(-3, 2) =>
str.slice(-3, -1) => og

substring Vs substr Vs slice
Though all the 3 functions are used to determine the substring but there is a difference between them. Below table summarizes the difference between these functions.

Area substr substring slice
Second argument meaning Second argument is the count of characters that are included in substring. Second argument is the index till which characters will be included. Second argument is the index till which characters will be included.
Optional Second argument Second argument is optional. Second argument is optional. Second argument is optional.
Working Returns the characters from first argument till the total number of characters are equal to second argument. Returns the characters between first and second argument.
Character at first argument index is included and second argument index is excluded.
Returns the characters between first and second argument.
Character at first argument index is included and second argument index is excluded.
NaN Any argument as NaN is treated as 0. Any argument as NaN is treated as 0. Any argument as NaN is treated as 0.
Negative second argument Negative second argument is treated as 0. Negative second argument is treated as 0. Negative second argument is counted from backwards.
First argument > Second argument Returns the number of characters equal to second argument starting from first argument. Swaps the two arguments and returns the string between them. Returns empty string.
First argument = Second argument Returns the number of characters equal to second argument starting from first argument. Returns empty string. Returns empty string.

Do not forget to hit the clap if the article was useful.

Exit mobile version