Get Part of String JavaScript

Get Part Of String Methods In JavaScript
Get Part of String Methods JS

In this article, we’ll look at different ways to get part of a string in JavaScript.

substring(startIndex, endIndex) and slice(startIndex, endIndex) are the build-in methods we’ll use to get part of a string in JavaScript:

  • Their start index of the first character of the extracted part. It is included in the returned substring.
  • Their end index indicates the end of the extracted part. It is NOT included in the returned substring.
  • They return a new string containing the extracted part of the string.
  • The slice() method takes negative arguments.
  • The substring() method treats its negative arguments as 0’s.

Get part of a string in JavaScript by index

As you can see in the above table, slice() and substring() behave almost the same way.

Imagine we have a string and want to get part of it from index 11 until the end of the given string

const yourString = 'My parents treated us all the same when we were kids.';
const firstSubSrtring = yourString.substring(11);
const secondSubString = yourString.slice(11);

//The results are the same
console.log('Using substring(): ' + firstSubSrtring); 
console.log('Using slice(): ' + secondSubString);

Output:

Using substring(): treated us all the same when we were kids.
Using slice(): treated us all the same when we were kids.

The index counting starts from 0, so the character at index 11 is ‘t‘ We didn’t supply the method with an end index, so it returned the substring from the start index to the end of the given string. As you can see, the result is identical for both methods.

We can also extract part of the string setting an end index:

const yourString = 'My parents treated us all the same when we were kids.';
const firstSubSrtring = yourString.substring(11, 25);
const secondSubString = yourString.slice(11, 45);


console.log('substring method: ' + firstSubSrtring);
console.log('slice method:' + secondSubString);

Output:

substring method: treated us all
slice method:treated us all the same when we we

Get part of a string after a specific character

If we have to get part of a string after a specific character, first of all, we need to find at which index the character resides using the indexof() method. Then we’ll increment by ‘1’ the value of the found index and put the result as an argument in substring() or slice().

indexOf() returns the first index at which a given substring can be found. If there’s no found value, it returns -1. The method is case-sensitive.

const yourString = 'My parents treated us all the same when we were kids.';
//get the index of the character 'w'
const indexOfChar = yourString.indexOf('w');

//get the index of the string 'when'
const indexOfString = yourString.indexOf('when');

console.log(indexOfChar); // --->  35
console.log(indexOfString); // 35

Output:

35
35

So whether you use the indexOf() method with one character or more, it will return the first index.

Now we have the index, so we can extract the substring:

const yourString = 'My parents treated us all the same when we were kids.';
//get the index of the character 'w'
const indexOfChar = yourString.indexOf('w');

// if the character was found in the given string
if(indexOfChar !== -1) {
   // we increment the index by 1 
   const substr = yourString.substring(indexOfChar + 1, 50);
   console.log(substr);
}

Output:

hen we were ki

The ‘w’ letter was excluded because we incremented the found index by 1.

Get part of a string between 2 characters (letters)

Let’s imagine we need to get part of the string between ‘t’ and ‘d’. We’ll use indexof() again for both characters to find their position.

const yourString = 'My parents treated us all the same when we were kids.';

const firstCharIndex = yourString.indexOf('t');
const secondCharIndex = yourString.indexOf('d');

// check if both letters exists in the string
if(firstCharIndex !== -1 && secondCharIndex !== -1 ) {
  // the character at secondCharIndex is exluded of the substring
  const substr = yourString.substring(firstCharIndex + 1, secondCharIndex);
  console.log(`The substring betweeen 't' letter and 'd' letter is:  ${substr}`);
}

Output:

The substring betweeen 't' letter and 'd' letter is:  s treate

Get part of a string after a word

We want to extract a part of the string ‘My parents treated us all the same.’ that is located after a specific word, for example, ‘treated‘ We need to find the index of the word, then calculate its amount of letter which we’ll use to get the exact index after the word.

Get part of string after word javascript
const yourString = 'My parents treated us all the same.';
const word = 'treated';
//gets the start position of the word
const wordIndex = yourString.indexOf(word);
//gets the length of the word
const numberOfCharacters = word.length;

//calculates the start positon of the substring
const substrStartIndex  = wordIndex + numberOfCharacters;
console.log(yourString.substring(substrStartIndex));

Keep in mind that the whitespace is included in the substring.

Output:

 us all the same when we were kids. 

Get part of a string between 2 words

To extract a part of a string between two words, you need to use the previous example where we found the start position of the extracted substring; afterward, we’ll get its end position by simply calling the indexOf() method.

const yourString = 'My parents treated us all the same when we were kids.';
const firstWord = 'treated';
const secondWord = 'all';
const firstWordIndex = yourString.indexOf(firstWord);
const numberOfCharacters = firstWord.length;


const secondWordIndex = yourString.indexOf(secondWord);

const substrStartIndex  = firstWordIndex + numberOfCharacters;
console.log(yourString.substring(substrStartIndex, secondWordIndex));

In addition, the output has a whitespace at the start and at the end:

 us 

Similar Posts