Results 1 to 13 of 13

Thread: Search and Replace code

  1. #1

    Thread Starter
    Hyperactive Member DarkX_Greece's Avatar
    Join Date
    Jan 2004
    Location
    Athens (Greece)
    Posts
    315

    Arrow Search and Replace code

    Hello, all!

    Can anyone, tell me a vb6 code, that will search for a word in a text box and then replace it with an other! Well, i know the replace method from vb but it is not to fast! The text is 2000 words big!

    Thanks in advance!
    Short CV:
    1. Visual Basic 6 Programmer
    2. Web Expert


    Botonakis Web Services

  2. #2
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: Search and Replace code

    Will the replaced text be equal, bigger or smaller in size?

    Anyways, you could try using byte arrays. Optimized and compiled, it can be a lot faster than other replacing styles.

  3. #3
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Search and Replace code

    REPLACE is quite easy to code - are you experiencing that bad a pause when you use it on 2000 words?

  4. #4
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Search and Replace code

    Quote Originally Posted by szlamany
    REPLACE is quite easy to code - are you experiencing that bad a pause when you use it on 2000 words?
    I was curious about the same thing: it's not really that slow as far as I am concern and is really (as szlamany pointed out) simple to use so why complicating things ... that's how I see it.

  5. #5
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,427

    Re: Search and Replace code

    I created a little test project and found that on my PC it only took less than 1ms to do a Replace on a textbox that containsed 2000 words and 16ms to do the same thing with 20,000 words.

  6. #6
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Search and Replace code

    Martin,

    Just out of curiosity - if you still have the test available.

    Sometimes it's more meaningful to see how many times you can do a REPLACE on 2000 words in a second - or let's say in 5 seconds - that microsecond means so little to the real world.

    That way the original poster can get a real feel for how many replaces can occur in a reasonable amount of time...

    Happy Holiday's...

  7. #7
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,427

    Re: Search and Replace code

    Add a multiline textbox and a command button to Form1. Note that it may take a few seconds to fill the textbox at startup.
    VB Code:
    1. Option Explicit
    2. Private Declare Function GetTickCount Lib "kernel32" () As Long
    3.  
    4. Private Sub Command1_Click()
    5.  
    6.     ' Place this code in any Sub or Function
    7.     Dim lngStart As Long
    8.     Dim lngFinish As Long
    9.     Dim lngCounterOne As Long
    10.     Dim lngCounterTwo As Long
    11.    
    12.     ' Record the start "time"
    13.     lngStart = GetTickCount()
    14.    
    15.     ' Time it
    16.     Text1.Text = Replace(Text1.Text, "little", "xxx")
    17.    
    18.     ' Record the finish "time"
    19.    
    20.     lngFinish = GetTickCount()
    21.    
    22.     ' Display the difference
    23.     MsgBox CStr(lngFinish - lngStart)
    24.  
    25. End Sub
    26.  
    27. Private Sub Form_Load()
    28. 'http://www.vbforums.com/showthread.php?t=317562
    29. Dim x As Integer
    30.  
    31. For x = 1 To 2000 ' Create 20,000 words
    32.     Text1.Text = Text1.Text & "This little sentence contains ten words, no more, no less. "
    33. Next
    34.  
    35. End Sub

  8. #8

    Thread Starter
    Hyperactive Member DarkX_Greece's Avatar
    Join Date
    Jan 2004
    Location
    Athens (Greece)
    Posts
    315

    Re: Search and Replace code

    Quote Originally Posted by MartinLiss
    Add a multiline textbox and a command button to Form1. Note that it may take a few seconds to fill the textbox at startup.
    VB Code:
    1. Option Explicit
    2. Private Declare Function GetTickCount Lib "kernel32" () As Long
    3.  
    4. Private Sub Command1_Click()
    5.  
    6.     ' Place this code in any Sub or Function
    7.     Dim lngStart As Long
    8.     Dim lngFinish As Long
    9.     Dim lngCounterOne As Long
    10.     Dim lngCounterTwo As Long
    11.    
    12.     ' Record the start "time"
    13.     lngStart = GetTickCount()
    14.    
    15.     ' Time it
    16.     Text1.Text = Replace(Text1.Text, "little", "xxx")
    17.    
    18.     ' Record the finish "time"
    19.    
    20.     lngFinish = GetTickCount()
    21.    
    22.     ' Display the difference
    23.     MsgBox CStr(lngFinish - lngStart)
    24.  
    25. End Sub
    26.  
    27. Private Sub Form_Load()
    28. 'http://www.vbforums.com/showthread.php?t=317562
    29. Dim x As Integer
    30.  
    31. For x = 1 To 2000 ' Create 20,000 words
    32.     Text1.Text = Text1.Text & "This little sentence contains ten words, no more, no less. "
    33. Next
    34.  
    35. End Sub
    Something for me? Maybe the Replace method it's not to slow, but is there any other way?
    Short CV:
    1. Visual Basic 6 Programmer
    2. Web Expert


    Botonakis Web Services

  9. #9

  10. #10

    Thread Starter
    Hyperactive Member DarkX_Greece's Avatar
    Join Date
    Jan 2004
    Location
    Athens (Greece)
    Posts
    315

    Re: Search and Replace code

    For using a faster code, for personal reasons!

    I know that there is faster code, so i hope someone will know it...
    Short CV:
    1. Visual Basic 6 Programmer
    2. Web Expert


    Botonakis Web Services

  11. #11
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Search and Replace code

    Quote Originally Posted by DarkX_Greece
    ... Maybe the Replace method it's not to slow, but is there any other way?
    Sure there is - write your own Replace() function in C++.
    But as Martin said "Why do you need another way?"

  12. #12

    Thread Starter
    Hyperactive Member DarkX_Greece's Avatar
    Join Date
    Jan 2004
    Location
    Athens (Greece)
    Posts
    315

    Re: Search and Replace code

    Quote Originally Posted by RhinoBull
    Sure there is - write your own Replace() function in C++.
    But as Martin said "Why do you need another way?"

    Yes, he said that, but as you can see, i replied with "For using a faster code, for personal reasons!"
    Short CV:
    1. Visual Basic 6 Programmer
    2. Web Expert


    Botonakis Web Services

  13. #13

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