Javascript substring

I have the line "2500 - SomeValue". How can I delete everything up to "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

+3
source share
6 answers

Add 3 (length "-") to the last index and leave the second parameter. By default, when passing one parameter to a substring, the end of the line goes

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

It's simple:

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

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

EDIT: Since you need everything starting with a "-", I added 3 to the starting point to skip these characters.

+3
source
x.substring(x.lastIndexOf(" - "),x.length-1); //corrected
0

var y = x.split(" ",2);

. y [2] , .

0
var y = x.substring(0, x.lastIndexOf(' - ') + 3)
0

var y = x.slice (x.lastIndexOf ("-"), x.length - 1);

This will return the value as a string, regardless of what value or how much time it has and has nothing to do with arrays.

0
source

Source: https://habr.com/ru/post/1715665/


All Articles