Hai all,

in my appliction Im writing functionality to find keywords entered by user
if keyword found i need to return the 10 words before keyword and 10 words after keywords, Im able to find the keywords using Instr method.. Im not getting how to get 10 words Before and after keywords.. Any one hae idea? if you have sample code Then it would more help full

Thanks In advance.

Recommended Answers

All 8 Replies

Here is one way.
Three textboxes and button.

Public Class Form1
    Dim KeyWord As String = "aid"

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        TextBox1.Text = "Now is the time for all good men to come to the aid of their country the quick brown fox jumps over the lazy dog"
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim Ary() As String = Nothing
        Ary = TextBox1.Text.Split(" ")
        Dim index As Integer = 0
        For index = 0 To UBound(Ary)
            If Ary(index) = KeyWord Then
                Exit For
            End If
        Next
        If index <= UBound(Ary) Then
            For i As Integer = index - 10 To index - 1
                TextBox2.Text &= Ary(i) & " "
            Next
            For i As Integer = index + 1 To index + 10
                TextBox3.Text &= Ary(i) & " "
            Next
        End If
    End Sub
End Class

Thank u. thats good idea.
But i need to find the paragraph.
in entire document there may be 100's of paragraph but i need to get only those paragraph in which the key word is found.....
pls help me out.....

thanks again..

Not clear -- you want to find 10 words before, and 10 after any keyword, but only if the words are inside a paragraph?

Like, the keyword is the firs word in a given paragraph, so you'd only want 10 words after?

You just need to search for the line break before, and the line break after, then only search inside that block. Then, search for 10 space characters before and after, and include every character between those two indexes.

Try this then:

Public Class Form1
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        TextBox1.Text = "The quick brown fox jumps over the lazy dog" & vbNewLine
        TextBox1.Text &= "Now is the time for all good men to come to the aid of their country" & vbNewLine
        TextBox1.Text &= "This is a test for finding all."
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim ary As String()
        ary = TextBox1.Text.Split(vbNewLine)
        For index As Integer = 0 To UBound(ary)
            If ary(index).IndexOf("the") <> -1 Then
                MessageBox.Show(ary(index))
            End If
        Next
    End Sub
End Class

Thank u both ken sharpe and waynespangler for ur response.

what exactly i want is .

Say in document there may be 100 paragrphs out of which only 10 paragraph contains the keyword for which im searching for. i have to return those 10 paragraphs which have the keyword. if keyword is at start of paragraph then it should pick that paragraph and keep serching for the next paragraphs in which key word is there untill it reaches the end of document.
Hope this is clear...
Pls share ur ideas.
Thanks

If you tried the last code I sent, you will see it does what you want.

It works. Thank u very much.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.