-
Dec 24th, 2004, 07:06 AM
#1
Thread Starter
Hyperactive Member
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!
-
Dec 24th, 2004, 09:43 AM
#2
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.
-
Dec 24th, 2004, 09:49 AM
#3
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?
-
Dec 24th, 2004, 10:12 AM
#4
Re: Search and Replace code
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.
-
Dec 24th, 2004, 11:57 AM
#5
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.
-
Dec 24th, 2004, 12:00 PM
#6
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...
-
Dec 24th, 2004, 12:04 PM
#7
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:
Option Explicit
Private Declare Function GetTickCount Lib "kernel32" () As Long
Private Sub Command1_Click()
' Place this code in any Sub or Function
Dim lngStart As Long
Dim lngFinish As Long
Dim lngCounterOne As Long
Dim lngCounterTwo As Long
' Record the start "time"
lngStart = GetTickCount()
' Time it
Text1.Text = Replace(Text1.Text, "little", "xxx")
' Record the finish "time"
lngFinish = GetTickCount()
' Display the difference
MsgBox CStr(lngFinish - lngStart)
End Sub
Private Sub Form_Load()
'http://www.vbforums.com/showthread.php?t=317562
Dim x As Integer
For x = 1 To 2000 ' Create 20,000 words
Text1.Text = Text1.Text & "This little sentence contains ten words, no more, no less. "
Next
End Sub
-
Dec 25th, 2004, 06:09 AM
#8
Thread Starter
Hyperactive Member
Re: Search and Replace code
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:
Option Explicit
Private Declare Function GetTickCount Lib "kernel32" () As Long
Private Sub Command1_Click()
' Place this code in any Sub or Function
Dim lngStart As Long
Dim lngFinish As Long
Dim lngCounterOne As Long
Dim lngCounterTwo As Long
' Record the start "time"
lngStart = GetTickCount()
' Time it
Text1.Text = Replace(Text1.Text, "little", "xxx")
' Record the finish "time"
lngFinish = GetTickCount()
' Display the difference
MsgBox CStr(lngFinish - lngStart)
End Sub
Private Sub Form_Load()
'http://www.vbforums.com/showthread.php?t=317562
Dim x As Integer
For x = 1 To 2000 ' Create 20,000 words
Text1.Text = Text1.Text & "This little sentence contains ten words, no more, no less. "
Next
End Sub
Something for me? Maybe the Replace method it's not to slow, but is there any other way?
-
Dec 25th, 2004, 09:55 AM
#9
Re: Search and Replace code
Why do you need another way?
-
Dec 25th, 2004, 10:02 AM
#10
Thread Starter
Hyperactive Member
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...
-
Dec 25th, 2004, 10:03 AM
#11
Re: Search and Replace code
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?"
-
Dec 25th, 2004, 10:09 AM
#12
Thread Starter
Hyperactive Member
Re: Search and Replace code
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!"
-
Dec 25th, 2004, 10:18 AM
#13
Re: Search and Replace code
If you were willing to tell us why, then maybe we could help you.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|