javascript substring help

11,394

Solution 1

Add 3 (the length of " - ") to the last index and leave off the second parameter. The default when you pass one parameter to substring is the go to the end of the string

var y = x.substring(x.lastIndexOf(" - ") + 3);

Solution 2

That's just:

var y = x.substring(x.lastIndexOf(" - ") + 3);

When you omit the second parameter, it just gives you everything to the end of the string.

EDIT: Since you wanted everything from after the " - " I've added 3 to the starting point in order to skip those characters.

Share:
11,394
Hcabnettek
Author by

Hcabnettek

@kettenbach

Updated on June 04, 2022

Comments

  • Hcabnettek
    Hcabnettek almost 2 years

    I have a string "2500 - SomeValue". How can I remove everything before the 'S' in 'SomeValue'?

    var x = "2500 - SomeValue";
    var y = x.substring(x.lastIndexOf(" - "),
    // this is where I'm stuck, I need the rest of the string starting from here. 
    

    Thanks for any help.

    ~ck