I have "Step-1", "Step-2"… etc string.
I want to get only the number. how can i get it ?
My solution:
var stepName = "Step-1";
var stepNumber = Integer.Parse(stepName[5]);
console.log(stepNumber);
but It is not good solution because stepName can be "Step-500".
please suggest me a way to get number after "-"
5
Answers
You can first use
match
and then useparseInt
to get numberYou can also use
split
here as:or
You can use this
I think the best solution is not by regex. If your text format would be a constant as
"Step-1"
and your just replacing the number from1 - 1********
. You can just split them in an array and get the second array.So what the code does is it split your string into two and assign it an array
PS. But again this will only work if you have a constant format of that variable. e.g "$var1-$var2"
You can do with replace function too, try below code