Results 1 to 3 of 3

Thread: instr backwards ?

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2003
    Posts
    5

    instr backwards ?

    I`m trying to write a program to search for a word in a sentence but backwards !!

    Take a scentence like this, I want to search for the word "ate" then extract the word monkey to the left of it.

    "The monkey ate all the bananas"

    For it to work in my program the way I want to I cant search between the words "The" & "ate" - hard to explain but i dont want it this way.

    What the program needs to do is find the word "Ate" then work backwards untill it gets to the word "the" in the sentence.

    as simply as possible, using either left,split,instr ect...

    cheerz

  2. #2
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    Really basic example, doesn't handle for the fact that the search term may be the first word...

    VB Code:
    1. Dim strTest As String, strArray() As String
    2.     strTest = "The monkey ate all the bananas"
    3.  
    4.     strArray = Split(strTest)
    5.    
    6.     For i = 0 To UBound(strArray)
    7.         If strArray(i) = "ate" Then
    8.             MsgBox strArray(i - 1)
    9.             Exit For
    10.         End If
    11.     Next

    hope that helps :/

  3. #3
    Lively Member
    Join Date
    Nov 2002
    Location
    Perth - Australia
    Posts
    105
    not sure if this is quite what you are looking for but the 'instr backwards' function is InStrRev

    Description

    Returns the position of an occurrence of one string within another, from the end of string.

    Syntax

    InstrRev(string1, string2[, start])

    The InstrRev function syntax has these parts:

    Part Description
    string1 Required.String expression being searched.
    string2 Required. String expression being searched for.
    start Optional.Numeric expression that sets the starting position for each search. If omitted, –1 is used, which means that the search begins at the last character position. If start contains
    Null, an error occurs.

    See MSDN for a more detailed description.

    Hope that helps


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