Languages
[Edit]
EN

JavaScript - get substring after character

3 points
Created by:
Jan-Alfaro
341

In this article, we would like to show you how to get substring after character using JavaScript.

Quick solution:

// ONLINE-RUNNER:browser;

const character = ':';
const string = 'user:password';

const index = string.indexOf(character);
const substring = string.substring(index + 1);

console.log(substring);  // password

 

Practical example

In this example, we create a reusable function that helps to get the substring after indicated character.

// ONLINE-RUNNER:browser;

const getSubstring = (string, character) => {
    const index = string.indexOf(character);
    return string.substring(index + 1);
};


// usage example:

const text1 = 'Get the value:value1';
const text2 = 'Get the value:value2';

const result1 = getSubstring(text1, ':');  // value1
const result2 = getSubstring(text2, ':');  // value2

console.log(result1);  // value1
console.log(result2);  // value2

See also

  1. JavaScript - get substring before character 
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join