Results 1 to 9 of 9

Thread: [2005] Locating strings

  1. #1

    Thread Starter
    Hyperactive Member JXDOS's Avatar
    Join Date
    Aug 2006
    Location
    Mars...
    Posts
    423

    Exclamation [2005] Locating strings

    I am trying to get the position of a certain string with in a textbox

    e.g.

    textbox1.text = "hello everyone, nice weather today!"

    if string1 = nice,
    position as integer will be 17

    How do I get this integer through code?
    If my post has been helpful, please rate it!

  2. #2
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: [2005] Locating strings

    look for indexof
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  3. #3
    Member
    Join Date
    May 2007
    Posts
    40

    Re: [2005] Locating strings

    You can use instr to return the position:
    Dim position as integer = instr(textbox1.text, string1)

    If you want it to be case insensitive then use:
    instr(textbox1.text, string1, CompareMethod.Text)

  4. #4
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: [2005] Locating strings

    instr - old school.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  5. #5
    Member
    Join Date
    May 2007
    Posts
    40

    Re: [2005] Locating strings

    instr may be old school but it does return the zero based position (17 in the example) - IndexOf returns 16.
    Not that it really matters so long as you know which you are getting.

  6. #6
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: [2005] Locating strings

    but did the OP know that (almost?) everything is zero-based, and therefore 16 was really the correct answer? instr returns a 1 Based value.
    Last edited by dbasnett; Oct 30th, 2008 at 07:22 AM.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  7. #7
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: [2005] Locating strings

    Code:
            Dim aString As String = "hello everyone, nice weather today!"
            Debug.WriteLine(InStr(aString, "nice").ToString)
            Debug.WriteLine(aString.IndexOf("nice").ToString)
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  8. #8
    Member
    Join Date
    May 2007
    Posts
    40

    Re: [2005] Locating strings

    instr returns a 1 Based value
    That's what I meant to say - got it backwards when I posted (not paying enough attention, sorry).

  9. #9

    Thread Starter
    Hyperactive Member JXDOS's Avatar
    Join Date
    Aug 2006
    Location
    Mars...
    Posts
    423

    Re: [2005] Locating strings

    Thanks to all for your help!
    If my post has been helpful, please rate it!

Posting Permissions

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



Click Here to Expand Forum to Full Width