Connecting Tech Pros Worldwide Forums | Help | Site Map

javascript lastindexof()

Newbie
 
Join Date: Aug 2007
Posts: 1
#1: Aug 1 '07
hi all.

the enter only one word in a text box. lastindexof method




my in input is : D:/folder name/

i want out put is D:/folder name

gits's Avatar
Moderator
 
Join Date: May 2007
Location: Munich, Germany
Posts: 4,136
#2: Aug 1 '07

re: javascript lastindexof()


hi ...

js is case-sensitive ... so use:

Expand|Select|Wrap|Line Numbers
  1. 'D:/folder name/'.lastIndexOf('/');
to get the position of the last slash ;)

now you have to remove the slash but you have to compare the length of your string with the 'slash-position' ... because i assume that you want to remove the ending slashes only?

or simply use the following for the task (that uses a regEx):

Expand|Select|Wrap|Line Numbers
  1. var foo = 'D:/folder name/'; 
  2. foo = foo.replace(/\/$/, '');
  3.  
kind regards
Reply