+ Reply to Thread
Results 1 to 11 of 11

InStr Question

  1. #1
    Norm Lundquist
    Guest

    InStr Question

    Here's an easy one that I can't see!

    Why does

    InStr(4,"XXpXXpXXPXXP","P",vbTextCompare)

    return 6 and

    Instr(5,"ABC-123-XYZ","-",vbTextCompare)

    return 8??? I thought the second one would return 4 because the second dash
    is the fourth character when you start search at position 5.

    THANKS!

  2. #2
    Die_Another_Day
    Guest

    Re: InStr Question

    InStr return the Absolute Position in the string, regardless of the
    "Start position" the Start position just tells it to skip the first few
    characters.

    Charles

    Norm Lundquist wrote:
    > Here's an easy one that I can't see!
    >
    > Why does
    >
    > InStr(4,"XXpXXpXXPXXP","P",vbTextCompare)
    >
    > return 6 and
    >
    > Instr(5,"ABC-123-XYZ","-",vbTextCompare)
    >
    > return 8??? I thought the second one would return 4 because the second dash
    > is the fourth character when you start search at position 5.
    >
    > THANKS!



  3. #3
    News
    Guest

    Re: InStr Question

    Instr return the position of the character from the beginning of the string,
    not relative to where you start.

    NickHK

    "Norm Lundquist" <[email protected]> 撰寫於郵件新聞:[email protected]...
    > Here's an easy one that I can't see!
    >
    > Why does
    >
    > InStr(4,"XXpXXpXXPXXP","P",vbTextCompare)
    >
    > return 6 and
    >
    > Instr(5,"ABC-123-XYZ","-",vbTextCompare)
    >
    > return 8??? I thought the second one would return 4 because the second
    > dash
    > is the fourth character when you start search at position 5.
    >
    > THANKS!




  4. #4
    Tom Ogilvy
    Guest

    RE: InStr Question

    It returns the position from the start of the string, not relative to where
    you start searching.

    --
    Regards,
    Tom Ogilvy


    "Norm Lundquist" wrote:

    > Here's an easy one that I can't see!
    >
    > Why does
    >
    > InStr(4,"XXpXXpXXPXXP","P",vbTextCompare)
    >
    > return 6 and
    >
    > Instr(5,"ABC-123-XYZ","-",vbTextCompare)
    >
    > return 8??? I thought the second one would return 4 because the second dash
    > is the fourth character when you start search at position 5.
    >
    > THANKS!


  5. #5
    Dave Peterson
    Guest

    Re: InStr Question

    Aren't they doing it exactly the same way. Returning the position of the
    character in that string--no matter where you started.

    It's not where you start the search, the function is returning the position in
    the string.



    Norm Lundquist wrote:
    >
    > Here's an easy one that I can't see!
    >
    > Why does
    >
    > InStr(4,"XXpXXpXXPXXP","P",vbTextCompare)
    >
    > return 6 and
    >
    > Instr(5,"ABC-123-XYZ","-",vbTextCompare)
    >
    > return 8??? I thought the second one would return 4 because the second dash
    > is the fourth character when you start search at position 5.
    >
    > THANKS!


    --

    Dave Peterson

  6. #6
    Bob Phillips
    Guest

    Re: InStr Question

    Because it returns its position in the string, not relative to the start
    point.

    --
    HTH

    Bob Phillips

    (replace somewhere in email address with gmail if mailing direct)

    "Norm Lundquist" <[email protected]> wrote in message
    news:[email protected]...
    > Here's an easy one that I can't see!
    >
    > Why does
    >
    > InStr(4,"XXpXXpXXPXXP","P",vbTextCompare)
    >
    > return 6 and
    >
    > Instr(5,"ABC-123-XYZ","-",vbTextCompare)
    >
    > return 8??? I thought the second one would return 4 because the second

    dash
    > is the fourth character when you start search at position 5.
    >
    > THANKS!




  7. #7
    Norm Lundquist
    Guest

    RE: InStr Question

    Tom,

    But why does the first example return 6 for the "P" when it is actually the
    9th character? I must REALLY be missing something!

    Norm

    "Tom Ogilvy" wrote:

    > It returns the position from the start of the string, not relative to where
    > you start searching.
    >
    > --
    > Regards,
    > Tom Ogilvy
    >
    >
    > "Norm Lundquist" wrote:
    >
    > > Here's an easy one that I can't see!
    > >
    > > Why does
    > >
    > > InStr(4,"XXpXXpXXPXXP","P",vbTextCompare)
    > >
    > > return 6 and
    > >
    > > Instr(5,"ABC-123-XYZ","-",vbTextCompare)
    > >
    > > return 8??? I thought the second one would return 4 because the second dash
    > > is the fourth character when you start search at position 5.
    > >
    > > THANKS!


  8. #8
    Norm Lundquist
    Guest

    RE: InStr Question

    Sorry for being so blind today! I was expecting it to find the capital P.

    I think I'll go crawl under a rock now!


    "Norm Lundquist" wrote:

    > Tom,
    >
    > But why does the first example return 6 for the "P" when it is actually the
    > 9th character? I must REALLY be missing something!
    >
    > Norm
    >
    > "Tom Ogilvy" wrote:
    >
    > > It returns the position from the start of the string, not relative to where
    > > you start searching.
    > >
    > > --
    > > Regards,
    > > Tom Ogilvy
    > >
    > >
    > > "Norm Lundquist" wrote:
    > >
    > > > Here's an easy one that I can't see!
    > > >
    > > > Why does
    > > >
    > > > InStr(4,"XXpXXpXXPXXP","P",vbTextCompare)
    > > >
    > > > return 6 and
    > > >
    > > > Instr(5,"ABC-123-XYZ","-",vbTextCompare)
    > > >
    > > > return 8??? I thought the second one would return 4 because the second dash
    > > > is the fourth character when you start search at position 5.
    > > >
    > > > THANKS!


  9. #9
    NickHK
    Guest

    Re: InStr Question

    See what value you get for:
    InStr(4,"XXpXXpXXPXXP","P",vbBinaryCompare)

    NickHK

    "Norm Lundquist" <[email protected]> 撰寫於郵件新聞:[email protected]...
    > Tom,
    >
    > But why does the first example return 6 for the "P" when it is actually
    > the
    > 9th character? I must REALLY be missing something!
    >
    > Norm
    >
    > "Tom Ogilvy" wrote:
    >
    >> It returns the position from the start of the string, not relative to
    >> where
    >> you start searching.
    >>
    >> --
    >> Regards,
    >> Tom Ogilvy
    >>
    >>
    >> "Norm Lundquist" wrote:
    >>
    >> > Here's an easy one that I can't see!
    >> >
    >> > Why does
    >> >
    >> > InStr(4,"XXpXXpXXPXXP","P",vbTextCompare)
    >> >
    >> > return 6 and
    >> >
    >> > Instr(5,"ABC-123-XYZ","-",vbTextCompare)
    >> >
    >> > return 8??? I thought the second one would return 4 because the second
    >> > dash
    >> > is the fourth character when you start search at position 5.
    >> >
    >> > THANKS!




  10. #10

    Re: InStr Question


    Norm Lundquist wrote:
    > Tom,
    >
    > But why does the first example return 6 for the "P" when it is actually the
    > 9th character? I must REALLY be missing something!
    >
    > Norm
    >


    Two things: InStr is maybe(?) NOT case-sensitive but DOES only begin to
    look where you tell it to... AND returns position from beginning of
    string (as prev. stated).

    Will


  11. #11
    Tom Ogilvy
    Guest

    RE: InStr Question

    use a binary compare if you want it to be case sensitive.

    --
    Regards,
    Tom Ogilvy


    "Norm Lundquist" wrote:

    > Sorry for being so blind today! I was expecting it to find the capital P.
    >
    > I think I'll go crawl under a rock now!
    >
    >
    > "Norm Lundquist" wrote:
    >
    > > Tom,
    > >
    > > But why does the first example return 6 for the "P" when it is actually the
    > > 9th character? I must REALLY be missing something!
    > >
    > > Norm
    > >
    > > "Tom Ogilvy" wrote:
    > >
    > > > It returns the position from the start of the string, not relative to where
    > > > you start searching.
    > > >
    > > > --
    > > > Regards,
    > > > Tom Ogilvy
    > > >
    > > >
    > > > "Norm Lundquist" wrote:
    > > >
    > > > > Here's an easy one that I can't see!
    > > > >
    > > > > Why does
    > > > >
    > > > > InStr(4,"XXpXXpXXPXXP","P",vbTextCompare)
    > > > >
    > > > > return 6 and
    > > > >
    > > > > Instr(5,"ABC-123-XYZ","-",vbTextCompare)
    > > > >
    > > > > return 8??? I thought the second one would return 4 because the second dash
    > > > > is the fourth character when you start search at position 5.
    > > > >
    > > > > THANKS!


+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1