Forum Moderators: open

Message Too Old, No Replies

Take 2

take last 2 characters

         

lihaze

2:34 pm on Jun 23, 2004 (gmt 0)



Do you know how I can take, for example, the last 2 characters of a string.

So if I have 2004 it will convert it to 04.

Like replace, but not!

thanks

Bernard Marx

4:48 pm on Jun 23, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




var str="hello"
alert(last(str,2))
// takes number or string
// returns string
function lastChars(inp,n)
{
inp = ""+inp // so it handles numbers too
return inp.substring(inp.length-n)
}