Results 1 to 4 of 4

Thread: [Resolved] Sub string within a string

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2007
    Posts
    10

    [Resolved] Sub string within a string

    Hi

    New to VB, but not to programming.

    I need to search lines of text for substrings.

    I recon LastIndexOfAny is the way to go.

    As an example, if I wanted to find the last occurance of the word "for"

    in: "This string is just for tests purposes and should not be taken for any other reasons."

    When I have tried various ways I get it returning an index of one of the letters of 'for' or at least that is what it looks like.

    I would be looking for a reply of 63 for the string above (presuming I counted corectly) as it's the last occurence of for. Not a return indicating the last occurance of 'o' instead or some other reply.

    Code:
    current_line = "This string is just for tests purposes and should not be used for any other reasons."
    count2 = current_line.LastIndexOfAny("for")
    Last edited by Writers Block; Jun 17th, 2007 at 05:43 PM. Reason: [Resolved]

  2. #2
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,950

    Re: Sub string within a string

    I get the result of 63 using "LastIndexOf" (the first character position is zero).

    Code:
            Dim s As String = "This string is just for tests purposes and should not be taken for any other reasons."
            MessageBox.Show(s.LastIndexOf("for"))

  3. #3

    Thread Starter
    New Member
    Join Date
    Jun 2007
    Posts
    10

    Re: Sub string within a string

    I get 80. As though it is returning the postion of a character in another word, not the whole word. And it is the whole word I am looking for.

  4. #4

    Thread Starter
    New Member
    Join Date
    Jun 2007
    Posts
    10

    Re: Sub string within a string

    No matter i feel stupid. Got it now.

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