Results 1 to 4 of 4

Thread: Getting word before string

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2000
    Posts
    10

    Question Getting word before string

    Hey.

    I'm wanting to be able to manipulate a string. I can search for the second word of the string, but I need to manipulate both the searched for word, and the word before it, let me show you an example...

    --------
    >
    Frosk asks, "any guards or colors?"
    >
    The dashing, jaunty song spins and dances as Zephore nimbly plays his flute.
    GSrE
    >
    Reznock smiles.
    >
    Lord Myrkull just arrived.
    Reznock nods to Hilaire.
    --------

    The string which I search for is 'asks, "', ans in this example I need to be able to manipulate the whole 'Frosk asks, "'.

    How would I go about finding this string?

    Thanks in advance.

    --DaveP

  2. #2
    Lively Member
    Join Date
    May 2001
    Location
    Leicester, UK
    Posts
    92
    Find out how far ASKS is into the string using LEN or something, then just take that and display those characters.

    eg.

    if its 10 letters into the string, then just take out the first 10

    Hope that helps

  3. #3
    Fanatic Member THEROB's Avatar
    Join Date
    Oct 2000
    Location
    I'm cold and there are wolves after me
    Posts
    575
    Try this:
    Code:
    Private Sub Command1_Click()
    Dim sString As String
    Dim sStringArray() As String
    sString = "Frosk asks, ""any guards or colors?"""
    
    'split the string into an array (only works in VB6)
    sStringArray = split(sString, " ")
    
    'Loop around looking for word to change
    For X = 0 To UBound(sStringArray)
        If sStringArray(X) = "asks," Then Exit For
    Next X
    
    'change the word and the word before it
    sStringArray(X - 1) = "Whatever" 'Change Frosk
    sStringArray(X) = "Whatever" 'Change asks
    
    For X = 0 To UBound(sStringArray)
    sString2 = sStringArray(X) & " "
    Next X
    
    End Sub
    Does this work - you may need to make it a tad more inteligent in picking words.

    Rob
    My secretary hopes that I will pay her, her landlord hopes that she will produce some rent, the Electricity Board hopes that he will settle their bill, and so on. I find it a wonderfully optimistic way of life. [Dirk Gently]

  4. #4

    Thread Starter
    New Member
    Join Date
    Jul 2000
    Posts
    10
    Thanks Rob!

    I used the basis of the code you supplied, and it works pretty well.

    It misses the occasional one, but only when I seem to get a rapid succession of matches.

    Thanks again

    --DaveP

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