Reputation:
I have this code:
var sequence = $stateParams.testId.length == 14 ?
parseInt($stateParams.testId.substring(11, 2)) : 0;
What I expect is for the sequence to be set to 01 (the last two numbers). But what happens is that it returns 198.
Can someone explain why this is?
console.log($stateParams.testId) // "01198-05282-01"
console.log(sequence) // 198
Upvotes: 2
Views: 1697
Reputation: 87203
The problem with substring()
is well-explained in this answer. If you want to get the last number after -
, you can use regex.
/\d+$/
Code:
var str = "01198-05282-01";
var sequence = (str.match(/\d+$/) || [])[0] || 0; // Get the last number, use 0 as default if not present
sequence = parseInt(sequence, 10);
What's wrong with substring()
?
There's nothing wrong in using substring()
but it has following limitations.
12
and 14
. One more thing to keep track of.Upvotes: 0
Reputation: 11707
Here is the explaination:
Your querystring contains: "01198-05282-01"
Lets take it in a variable:
var a= "01198-05282-01";
If we substring it,
a.substring(11,2)
It will return:
"198-05282"
You have parseInt in your code, hence
parseInt("198-05282");
will return
198
The substring() method returns a subset of a string between one index and another, or through the end of the string.
So to get the desired output you need to,
var a= "01198-05282-01";
console.log(a.substring(12,14)) //returns "01"
with parseInt(a.substring(12,14),10) //will return 1
With your code:
var sequence = $stateParams.testId.length == 14 ?
parseInt($stateParams.testId.substring(12, 14), 10) : 0;
As
More about substring: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/substring
As Tushar suggessted use radix as second parameter with parseInt
The parseInt() function parses a string argument and returns an integer of the specified radix (the base in mathematical numeral systems).
radix: An integer between 2 and 36 that represents the radix (the base in mathematical numeral systems) of the above mentioned string. Specify 10 for the decimal numeral system commonly used by humans. Always specify this parameter to eliminate reader confusion and to guarantee predictable behavior. Different implementations produce different results when a radix is not specified, usually defaulting the value to 10.
Upvotes: 0
Reputation: 87203
String#substring
works with the indexes.
The signature of the substring
is
str.substring(indexStart[, indexEnd])
Note that the second parameter is the index itself and not the length of the substring.
If
indexStart
is greater thanindexEnd
, then the effect ofsubstring()
is as if the two arguments were swapped; for example,str.substring(1, 0) == str.substring(0, 1)
.
So,
.substring(11, 2)
is equivalent to .substring(11, 2)
which will give the substring "198-05282-"
and using parseInt()
on it will give the output as 198
.
parseInt
as 10 to avoid unexpected resultsCode:
var str = '01198-05282-01';
var sequence = str.length == 14 ?
parseInt(str.substring(12, 14), 10) : 0;
// ^^ ^^
console.log(sequence);
To get the substring using the length
, use String#substr
str.substr(12, 2)
Upvotes: 1