Results 1 to 11 of 11

Thread: search text document

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2003
    Posts
    5

    search text document

    hi im new to vb.net, i wanted to know how to search a text doc for a user entered character and another search for a user entered word. if the character or word exists, the user needs to be prompted with a message box. i can open a text doc using FileStream and get a message box to appear but im unsure how to find the character or word in the doc.

    Cheers
    Snoopy

  2. #2
    Fanatic Member
    Join Date
    Sep 2002
    Posts
    518
    Check out the String.IndexOf method:

    ms-help://MS.VSCC/MS.MSDNVS/cpref/html/frlrfSystemStringClassIndexOfTopic.htm

    e.g. something like
    Code:
    Dim strFoo As String = "somestring"
    Dim strStringToFind As String = "str"
    Dim iIndex As Integer
    
    iIndex = strFoo.IndexOf(strStringToFind)  'iIndex = 4
    MsgBox("Found string at position " + iIndex)

  3. #3

    Thread Starter
    New Member
    Join Date
    Apr 2003
    Posts
    5
    Originally posted by Slow_Learner
    Check out the String.IndexOf method:

    ms-help://MS.VSCC/MS.MSDNVS/cpref/html/frlrfSystemStringClassIndexOfTopic.htm

    e.g. something like
    Code:
    Dim strFoo As String = "somestring"
    Dim strStringToFind As String = "str"
    Dim iIndex As Integer
    
    iIndex = strFoo.IndexOf(strStringToFind)  'iIndex = 4
    MsgBox("Found string at position " + iIndex)
    I tried out your code, this isn't for vb.net is it? I renamed msgbox to messagebox.show bla bla..but it didnt understand the +iIndex) at the end of the line...

    its sounds like ur on the right lines (sorry, i have no idea with this heh task of mine)

    Cheers
    Snoopy

  4. #4
    Fanatic Member
    Join Date
    Sep 2002
    Posts
    518
    Well sorry, there was a little bug with the MsgBox line (forgot to convert iIndex to string).

    Here's a little project with just this code in it:
    Attached Files Attached Files

  5. #5

    Thread Starter
    New Member
    Join Date
    Apr 2003
    Posts
    5

    how would i get the program to tell me how many times that word is present in the doc

    I have sorted the word search out now! thanks a lot!!! (for sending that string demo)

    ========================================
    So if i searched for a word how would i get the program to tell me how many times that word is present in the document?
    Last edited by snoopy03; Apr 22nd, 2003 at 10:55 AM.

  6. #6
    Fanatic Member
    Join Date
    Sep 2002
    Posts
    518
    Something like this will work:
    Code:
            Dim strFoo As String = "aaabbbaaabbbaaabbbaaabbbaaabbbaaabbb"
            Dim strStringToFind As String = "aaa"
            Dim iStartIndex, iFound, iCount As Integer
    
            iStartIndex = 0
    
            Do While iFound <> -1
                iFound = strFoo.IndexOf(strStringToFind, iStartIndex)
                If iFound <> -1 Then
                    iStartIndex = iFound + strStringToFind.Length
                    iCount += 1
                End If
            Loop
            MsgBox("Found string " + CStr(iCount) + " times.")
    More complex fiddling with text, especially long strings of text, is probably better done with Regular Expressions. Read:
    ms-help://MS.VSCC/MS.MSDNVS/cpguide/html/cpconcomregularexpressions.htm
    ms-help://MS.VSCC/MS.MSDNVS/cpguide/html/cpconusingregularexpressionclasses.htm

  7. #7

    Thread Starter
    New Member
    Join Date
    Apr 2003
    Posts
    5
    (i dont know if you got the message)

    do u have any suggestions; if the word was present in the document; display the word with and without its vowels? getting tough now

    Thanks for any help

    Snoopy

  8. #8
    Fanatic Member
    Join Date
    Sep 2002
    Posts
    518
    Read the RegEx samples:
    ms-help://MS.VSCC/MS.MSDNVS/Cpqstart/html/cpsmpnetsamples-howtoregularexpressions.htm

    Your problem isn't very hard (take a look at the RegexReplace sample). Time for some elbow grease.

  9. #9

    Thread Starter
    New Member
    Join Date
    Apr 2003
    Posts
    5
    hey i had a look at that link it looks a bit complicated and it looks like it deals with nos..maybe it works with letters to..
    I found another suggestion (repeating the process for all vowels):

    MyString.Remove(CChar("a"), "")

    MyString.Remove(CChar("a"), CChar(""))

    but the error msg appeared:

    An unhandled exception of type 'System.InvalidCastException' occurred in microsoft.visualbasic.dll

    Additional information: Cast from string "a" to type 'Integer' is not valid.

    then i tried this instead, it didnt like this at all

    MyString.Remove("a", "")

    hmm any suggestions ?

    Snoopy

  10. #10
    Fanatic Member
    Join Date
    Sep 2002
    Posts
    518
    MyString.Remove(CChar("a"), "")

    Why are you trying to convert "a" to character? Also why are you trying to give String.Remove a character anyhow? Read the docs for String.Remove:

    ms-help://MS.VSCC/MS.MSDNVS/cpref/html/frlrfsystemstringclassremovetopic.htm

    Note what it expects as parameters.

  11. #11
    Lively Member
    Join Date
    Feb 2003
    Location
    UK
    Posts
    95
    Regular Expressions arnt that hard once you get your teeth into them and they're very powerful as well. Just give em a chance.

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