View Full Version : search backwards through a string
Dan Pick
06-27-2002, 02:16 PM
Hi All - I'm a JavaScript novice who has just started to manipluate strings. Does anyone know
how I search backwards through a string to find the last instance of a forward-slash in the
string. I then want to retrieve all of the text before that last instance of a forward-slash.
IE- i want to get "http://workflow/ACR.nsf" from "http://workflow/ACR.nsf/Goodish+Dan?OpenForm"
or another example would be i want to get "a/b/c/d/e/f/g" from "a/b/c/d/e/f/g/h"
any help would be gratefully recieved - thanks
Roelf
06-27-2002, 02:22 PM
you can use the lastIndexOf() method, it returns the guess what: last index of the searchstring in the string
Dan Pick
06-27-2002, 02:48 PM
Thanks very much for your reply Roel - i don't suppose that you could show me an example of this in action could you - I amvery much a JS novice.
thanks again
Lee Brenner
06-27-2002, 03:01 PM
myString = "a/b/c/d/e/f/g/h" ;
lastInstance = myString.lastIndexOf("/");
newString= myString.slice(0, lastInstance-1);
RadarBob
06-27-2002, 03:25 PM
Here's the idea. I make no claims that this code snippit is debugged and actually works (but don't let that stop you!).
There are probably several ways to skin the cat, this is just my suggestion. In general the solution should be a separate function with parameters for the string you're searching and the character you're searching for. Then return the substring. Don't forget to account for the possibility of not finding the search character in the string.
NOTE: the coding style is what you might see in C code. But in general "hard exiting" (via the RETURN) in the middle of a loop is considered bad coding technique and is definitely NOT "structured programming"
function grabSubstring (targetString, SearchCharacter) {
var foundString = new String ("");
var loopIndex = 0;
for (loopIndex = (lastindexof (targetString), loopIndex <= 1; loopIndex--) {
if (targetString[loopIndex] == SearchCharacter) {
foundString = targetString.substring (0,loopIndex-1)
// don't grab the character itself
return foundString;
}
/*
if the search string consists of only the search character itself,
this may fail because "loopIndex-1" becomes -1 which is outside
a string index range. Will have to think about how to solve that.
*/
} // end for loop
return foundString;
// will return an empty string if we didn't find anything.
}
Lee Brenner
06-27-2002, 03:46 PM
RadarBob, I believe "lastIndexOf" has to be called by an object and if it doesn't find the search value it returns -1 to let you know:
myString.lastIndexOf(searchValue);
But if you do want to do it yourself:
//If sourceString has 1 or less characters the for loop will never be
//executed...which is what you want to have happen.
function getAString(sourceString, searchChar){
resultString = " ";
for(positionIterator = (sourceString.length - 1); positionIterator >= 1; positionIterator--){
if(sourceString[positionIterator] == searchChar){
return resultString = sourceString.slice(0, positionIterator);
}//if
}//for
return resultString;
}//function
Lee Brenner
06-27-2002, 03:46 PM
double post for some reason
RadarBob
06-27-2002, 05:07 PM
Lee;
Ok, I'm using lastIndexOf () wrong. So use the "length" property instead. That will work. To wit:
for (loopIndex = targetString.length - 1, loopIndex <= 0; loopIndex--)
Lee Brenner
06-27-2002, 05:28 PM
Yup! And make it ">= 1" instead of "<=0" and you are good to go.
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.