How can i extract from string only last number which should be "5"?
var str = "1000040928423195 points added to your balance";
var str = parseInt(str);
var lastNum = str.substr(str.length - 1);
console.log(lastNum);
Since the indexing starts from 0 so use str. charAt(str. length-1) to get the last character of string.
To find last digit of a number, we use modulo operator %. When modulo divided by 10 returns its last digit. To find first digit of a number is little expensive than last digit. To find first digit of a number we divide the given number by 10 until number is greater than 10.
To get the last digit of a number in Python: Access the string representation of the number. Get the last character of the string representation. Convert the character to an integer.
Given your string...
var str = "1000040928423195 points added to your balance";
... we can extract all the numbers with a regex...
var onlyNumbers = str.replace(/\D/g,'');
... and, finally, get the last one:
var lastNumber = onlyNumbers.substring(onlyNumbers.length - 1);
Here is a demo:
var str = "1000040928423195 points added to your balance";
var onlyNumbers = str.replace(/\D/g,'');
var lastNumber = onlyNumbers.substring(onlyNumbers.length - 1);
console.log(lastNumber);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With