Thursday, November 15, 2007

javascript right string function

A couple of times I have needed to slice the right part of a string based on a delimiting character. I performed a couple of searches looking for a function to place in my toolbox, but came up empty. Below is my creation.


function rightstr(s,c)
{
L = s.lastIndexOf(c);
L=L+1;
return s.slice(L);
}

A couple of examples of how this function can be used:

rightstr('images/ico_note.gif','\')
This will return ico_note.gif

or for grabbing the value of a querystring value at the end of a url:
rightstr('http://tvplanner.comcast.net/?initView=schedule','=')
This will return schedule

0 comments: