Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New to the CF scene
    Join Date
    Jun 2002
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    search backwards through a string

    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

  • #2
    Senior Coder
    Join Date
    Jun 2002
    Location
    Zwolle, The Netherlands
    Posts
    1,120
    Thanks
    2
    Thanked 31 Times in 31 Posts
    you can use the lastIndexOf() method, it returns the guess what: last index of the searchstring in the string

  • #3
    New to the CF scene
    Join Date
    Jun 2002
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    thanks

    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

  • #4
    New Coder
    Join Date
    Jun 2002
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts
    myString = "a/b/c/d/e/f/g/h" ;
    lastInstance = myString.lastIndexOf("/");
    newString= myString.slice(0, lastInstance-1);

  • #5
    Regular Coder
    Join Date
    Jun 2002
    Location
    Round Rock, Texas
    Posts
    443
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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"


    Code:
    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.
    
    
    }
    Last edited by RadarBob; 06-27-2002 at 04:29 PM.

  • #6
    New Coder
    Join Date
    Jun 2002
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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:

    Code:
    //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
    Last edited by Lee Brenner; 06-27-2002 at 06:31 PM.

  • #7
    New Coder
    Join Date
    Jun 2002
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts
    double post for some reason
    Last edited by Lee Brenner; 06-27-2002 at 04:58 PM.

  • #8
    Regular Coder
    Join Date
    Jun 2002
    Location
    Round Rock, Texas
    Posts
    443
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Lee;
    Ok, I'm using lastIndexOf () wrong. So use the "length" property instead. That will work. To wit:

    Code:
    for (loopIndex = targetString.length - 1, loopIndex <= 0; loopIndex--)
    Last edited by RadarBob; 06-27-2002 at 06:16 PM.

  • #9
    New Coder
    Join Date
    Jun 2002
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yup! And make it ">= 1" instead of "<=0" and you are good to go.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •