Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What String.substr(negative number) does?

I found this snippet, but I am not sure if it should have -5 or -6, because I do not what exactly substr(negative) does.

('00000'+(15).toString(16)).substr(-5)
like image 713
exebook Avatar asked Mar 07 '26 14:03

exebook


1 Answers

substr() returns the a string made up of the last N characters, when -N is passed to it.

"Hello".substr(-2) => "lo"

From the docs:

If start is negative, substr uses it as a character index from the end of the string. If start is negative and abs(start) is larger than the length of the string, substr uses 0 as the start index.

like image 77
techfoobar Avatar answered Mar 09 '26 02:03

techfoobar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!