How to go backward direction in any string

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • veer
    New Member
    • Jul 2007
    • 198

    How to go backward direction in any string

    Hi
    i want to move in a back direction in any string
    just like substring function in which we can move from a particular location to another location in forward direction but i want to move backword direction

    can you suggest me any way how ican do this

    varinder
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by veer
    Hi
    i want to move in a back direction in any string
    just like substring function in which we can move from a particular location to another location in forward direction but i want to move backword direction

    can you suggest me any way how ican do this

    varinder
    Strings have an index just like an array which you can use to retrieve any character in the string.

    Comment

    • jamesd0142
      Contributor
      • Sep 2007
      • 471

      #3
      Take a look at the mid() function... I think this might solve your query

      Comment

      • stoogots2
        New Member
        • Sep 2007
        • 77

        #4
        If you are trying to capture a part of the string from some position within the string backwards to the beginning, you could use the length property (or length-index), and a for/next loop in VB. This is only a logical solution if you do know the relative position within the string where you want to start but do not know how many characters you want.

        VB
        Code:
         for n = length-1 to 0 Step -1 
        newstring=oldstring[n]+newstring
        next n
        //This captures everything from the end of the string (going backwards) until
        // it encounters a space
        C#
        Code:
         for (int n=length-1;n>=0;n--) 
        {
        if(oldstring[n]=" ")
        {
        break;
        }
        newstring=oldstring[n]+newstring;
        }
        Last edited by DrBunchman; Jun 5 '08, 11:46 AM. Reason: Added code tags - Please use the # button

        Comment

        • Plater
          Recognized Expert Expert
          • Apr 2007
          • 7872

          #5
          Originally posted by veer
          Hi
          i want to move in a back direction in any string
          just like substring function in which we can move from a particular location to another location in forward direction but i want to move backword direction

          can you suggest me any way how ican do this

          varinder
          So if a string was
          "A man and his dog went walking"

          And you wanted to start on the h of "his" and go backwards 5 characters. do you want returned string to be "and h" or "h dna"(reversed) .
          One can be done easily with substring(), the other would be better with the for loop

          Comment

          Working...