Web Analytics Made Easy -
StatCounter search backwards through a string - CodingForum

Announcement

Collapse
No announcement yet.

search backwards through a string

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new 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
    you can use the lastIndexOf() method, it returns the guess what: last index of the searchstring in the string
    I am the luckiest man in the world

    Comment


    • #3
      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

      Comment


      • #4
        myString = "a/b/c/d/e/f/g/h" ;
        lastInstance = myString.lastIndexOf("/");
        newString= myString.slice(0, lastInstance-1);

        Comment


        • #5
          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; Jun 27, 2002, 10:29 AM.

          Comment


          • #6
            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; Jun 27, 2002, 12:31 PM.

            Comment


            • #7
              double post for some reason
              Last edited by Lee Brenner; Jun 27, 2002, 10:58 AM.

              Comment


              • #8
                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; Jun 27, 2002, 12:16 PM.

                Comment


                • #9
                  Yup! And make it ">= 1" instead of "<=0" and you are good to go.

                  Comment

                  Working...
                  X