문제

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)
도움이 되었습니까?

해결책

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.

다른 팁

In general:

str.substr(-n)

Is the same as:

str.substr(str.length-n)

Which is the same as:

str.substr(str.length-n,str.length)

Which returns the sub-string:

str[str.length-n,...,str.length-1]
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top