Results 1 to 4 of 4

Thread: [RESOLVED] Replace the first occurrence of the word in text

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Dec 2009
    Posts
    99

    Resolved [RESOLVED] Replace the first occurrence of the word in text

    ok have a list of 84000 words , and have some articles in these articles i want to replace first occurrence of each word i have in listbox e.g

    Code:
               For Each item In ListBox1.Items
                Dim mytext As String = "My some text this text is very long text and i want to replace this"
                If ListBox1.Items.Contains(mytext) Then
                    mytext = Replace(mytext, mytext, "String to replace")
                End If
            Next
    but it used to replace the whole mytext i want to replace words in mytext and also it hang the system and very very slow and replace all occurrences i want to replace just the first occurrence any help or idea please.

  2. #2
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: Replace the first occurrence of the word in text

    To replace the first occurrence use:
    vb Code:
    1. mytext = Replace(mytext, mytext, "String to replace", 1, 1)

    To replace the first two occurrences use:
    vb Code:
    1. mytext = Replace(mytext, mytext, "String to replace", 1, 2)

    etc...



  3. #3

    Thread Starter
    Lively Member
    Join Date
    Dec 2009
    Posts
    99

    Re: Replace the first occurrence of the word in text

    Thanks. how to search for the word from listbox? problem is i have 84000 words in listbox so it take too much time for each word

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Replace the first occurrence of the word in text

    try this:

    vb Code:
    1. Dim mytext As String = "My some text this text is very long text and i want to replace this"
    2.    
    3. For Each item In ListBox1.Items
    4.     If mytext.Contains(item.ToString) Then
    5.         mytext = Replace(mytext, item.ToString, "replacement text", , 1)
    6.     End If
    7. Next

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