472,782 Members | 1,024 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,782 software developers and data experts.

Replace methode, Replace Function, Stringbuilder replace, Regex Replace, Split

Cor
Hi Newsgroup,

I have given an answer in this newsgroup about a "Replace".

There came an answer on that I did not understand, so I have done some
tests.

I got the idea that someone said, that the split method and the
regex.replace method was better than the string.replace method and replace
function. I did not believe that.

I have tested this in two ways: with iteration of small strings and with a
long string. (Because that I myself use often the Stringbuilder replace,
have I added that too).

My results where comparative in spended time (not very scientific done) and
the 1 as basis.

Small strings
VB replace 4
String replace 1
Stringbuilder replace 2
Split 6
Regex 25
Long string
VB replace 170
String replace 1
Stringbuilder 2
Split 160
Regex 16

Who will check if my test program is right and get the same results?

Cor

Public Module Main
Public Sub Main()
Dim max As Integer = 20000
Dim oldstring As String = "**item1%%**item2%%**item3%%"
Dim newstring As String
Dim myarray() As String
Dim sb As New System.Text.StringBuilder
Dim StartTick As Integer = Environment.TickCount
For i As Integer = 0 To max
Next
Dim rest As Integer = Environment.TickCount - StartTick
StartTick = Environment.TickCount
For i As Integer = 0 To max
newstring = Replace(oldstring, "%%", "")
Next
Debug.WriteLine((Environment.TickCount - StartTick - rest).ToString
_
& " " & newstring, "Replace")
StartTick = Environment.TickCount
For i As Integer = 0 To max
newstring = oldstring.Replace("%%", "")
Next
Debug.WriteLine((Environment.TickCount - StartTick - rest).ToString
& _
" " & newstring, "String.Replace")
StartTick = Environment.TickCount
For i As Integer = 0 To max
sb = New System.Text.StringBuilder(oldstring)
newstring = sb.Replace("%%", "").ToString
Next
Debug.WriteLine((Environment.TickCount - StartTick - rest).ToString
& _
" " & newstring, "Stringbuilder.Replace")
StartTick = Environment.TickCount
For i As Integer = 0 To max
myarray = Split(oldstring, "%%", , CompareMethod.Text)
newstring = String.Join("", myarray)

Next
Debug.WriteLine((Environment.TickCount - StartTick - rest).ToString
& _
" " & newstring, "Split")

StartTick = Environment.TickCount
For i As Integer = 0 To max
newstring =
System.Text.RegularExpressions.Regex.Replace(oldst ring, "%%", "")
Next
Debug.WriteLine((Environment.TickCount - StartTick - rest).ToString
& _
" " & newstring, "Regex.Replace")
Debug.WriteLine("----------------- now with long
string -------------")
sb = New System.Text.StringBuilder("")
For i As Integer = 0 To max
sb.Append(oldstring)
Next
oldstring = sb.ToString
StartTick = Environment.TickCount
newstring = Replace(oldstring, "%%", "")
Debug.WriteLine((Environment.TickCount - StartTick).ToString _
& " " & newstring.Substring(0, 20), "Replace")
StartTick = Environment.TickCount
newstring = oldstring.Replace("%%", "")
Debug.WriteLine((Environment.TickCount - StartTick).ToString & _
" " & newstring.Substring(0, 20), "String.Replace")
StartTick = Environment.TickCount
sb = New System.Text.StringBuilder(oldstring)
newstring = sb.Replace("%%", "").ToString
Debug.WriteLine((Environment.TickCount - StartTick).ToString & _
" " & newstring.Substring(0, 20), "Stringbuilder.Replace")
StartTick = Environment.TickCount
myarray = Split(oldstring, "%%", , CompareMethod.Text)
newstring = String.Join("", myarray)
Debug.WriteLine((Environment.TickCount - StartTick).ToString & _
" " & newstring.Substring(0, 20), "Split")
StartTick = Environment.TickCount
newstring = System.Text.RegularExpressions.Regex.Replace(oldst ring,
"%%", "")
Debug.WriteLine((Environment.TickCount - StartTick).ToString & _
" " & newstring.Substring(0, 20), "Regex.Replace")
End Sub


Nov 20 '05 #1
4 3770
here you go

Replace: 62 **item1**item2**item3
String.Replace: 16 **item1**item2**item3
Stringbuilder.Replace: 16 **item1**item2**item3
Split: 46 **item1**item2**item3
Regex.Replace: 235 **item1**item2**item3
----------------- now with long string -------------
Replace: 1454 **item1**item2**item
String.Replace: 15 **item1**item2**item
Stringbuilder.Replace: 16 **item1**item2**item
Split: 1172 **item1**item2**item
Regex.Replace: 140 **item1**item2**item

"Cor" <no*@non.com> wrote in message
news:uM**************@tk2msftngp13.phx.gbl...
Hi Newsgroup,

I have given an answer in this newsgroup about a "Replace".

There came an answer on that I did not understand, so I have done some
tests.

I got the idea that someone said, that the split method and the
regex.replace method was better than the string.replace method and replace
function. I did not believe that.

I have tested this in two ways: with iteration of small strings and with a
long string. (Because that I myself use often the Stringbuilder replace,
have I added that too).

My results where comparative in spended time (not very scientific done) and the 1 as basis.

Small strings
VB replace 4
String replace 1
Stringbuilder replace 2
Split 6
Regex 25
Long string
VB replace 170
String replace 1
Stringbuilder 2
Split 160
Regex 16

Who will check if my test program is right and get the same results?

Cor

Public Module Main
Public Sub Main()
Dim max As Integer = 20000
Dim oldstring As String = "**item1%%**item2%%**item3%%"
Dim newstring As String
Dim myarray() As String
Dim sb As New System.Text.StringBuilder
Dim StartTick As Integer = Environment.TickCount
For i As Integer = 0 To max
Next
Dim rest As Integer = Environment.TickCount - StartTick
StartTick = Environment.TickCount
For i As Integer = 0 To max
newstring = Replace(oldstring, "%%", "")
Next
Debug.WriteLine((Environment.TickCount - StartTick - rest).ToString _
& " " & newstring, "Replace")
StartTick = Environment.TickCount
For i As Integer = 0 To max
newstring = oldstring.Replace("%%", "")
Next
Debug.WriteLine((Environment.TickCount - StartTick - rest).ToString & _
" " & newstring, "String.Replace")
StartTick = Environment.TickCount
For i As Integer = 0 To max
sb = New System.Text.StringBuilder(oldstring)
newstring = sb.Replace("%%", "").ToString
Next
Debug.WriteLine((Environment.TickCount - StartTick - rest).ToString & _
" " & newstring, "Stringbuilder.Replace")
StartTick = Environment.TickCount
For i As Integer = 0 To max
myarray = Split(oldstring, "%%", , CompareMethod.Text)
newstring = String.Join("", myarray)

Next
Debug.WriteLine((Environment.TickCount - StartTick - rest).ToString & _
" " & newstring, "Split")

StartTick = Environment.TickCount
For i As Integer = 0 To max
newstring =
System.Text.RegularExpressions.Regex.Replace(oldst ring, "%%", "")
Next
Debug.WriteLine((Environment.TickCount - StartTick - rest).ToString & _
" " & newstring, "Regex.Replace")
Debug.WriteLine("----------------- now with long
string -------------")
sb = New System.Text.StringBuilder("")
For i As Integer = 0 To max
sb.Append(oldstring)
Next
oldstring = sb.ToString
StartTick = Environment.TickCount
newstring = Replace(oldstring, "%%", "")
Debug.WriteLine((Environment.TickCount - StartTick).ToString _
& " " & newstring.Substring(0, 20), "Replace")
StartTick = Environment.TickCount
newstring = oldstring.Replace("%%", "")
Debug.WriteLine((Environment.TickCount - StartTick).ToString & _
" " & newstring.Substring(0, 20), "String.Replace")
StartTick = Environment.TickCount
sb = New System.Text.StringBuilder(oldstring)
newstring = sb.Replace("%%", "").ToString
Debug.WriteLine((Environment.TickCount - StartTick).ToString & _
" " & newstring.Substring(0, 20), "Stringbuilder.Replace")
StartTick = Environment.TickCount
myarray = Split(oldstring, "%%", , CompareMethod.Text)
newstring = String.Join("", myarray)
Debug.WriteLine((Environment.TickCount - StartTick).ToString & _
" " & newstring.Substring(0, 20), "Split")
StartTick = Environment.TickCount
newstring = System.Text.RegularExpressions.Regex.Replace(oldst ring, "%%", "")
Debug.WriteLine((Environment.TickCount - StartTick).ToString & _
" " & newstring.Substring(0, 20), "Regex.Replace")
End Sub


Nov 20 '05 #2
max 200000
Replace: 390 **item1**item2**item3
String.Replace: 125 **item1**item2**item3
Stringbuilder.Replace: 203 **item1**item2**item3
Split: 516 **item1**item2**item3
Regex.Replace: 2469 **item1**item2**item3
----------------- now with long string -------------
Replace: 209297 **item1**item2**item
String.Replace: 94 **item1**item2**item
Stringbuilder.Replace: 156 **item1**item2**item
Split: 179563 **item1**item2**item
Regex.Replace: 1375 **item1**item2**item
"EricJ" <er********@ThiSbitconsult.be.RE> wrote in message
news:40**********************@news.skynet.be...
here you go

Replace: 62 **item1**item2**item3
String.Replace: 16 **item1**item2**item3
Stringbuilder.Replace: 16 **item1**item2**item3
Split: 46 **item1**item2**item3
Regex.Replace: 235 **item1**item2**item3
----------------- now with long string -------------
Replace: 1454 **item1**item2**item
String.Replace: 15 **item1**item2**item
Stringbuilder.Replace: 16 **item1**item2**item
Split: 1172 **item1**item2**item
Regex.Replace: 140 **item1**item2**item

Nov 20 '05 #3
Here ya go
Replace: 31 **item1**item2**item3

String.Replace: 16 **item1**item2**item3

Stringbuilder.Replace: 15 **item1**item2**item3

Split: 63 **item1**item2**item3

Regex.Replace: 266 **item1**item2**item3

----------------- now with longstring -------------

Replace: 1250 **item1**item2**item

String.Replace: 16 **item1**item2**item

Stringbuilder.Replace: 15 **item1**item2**item

Split: 985 **item1**item2**item

Regex.Replace: 109 **item1**item2**item

The program '[5092] speedtest1.exe' has exited with code 0 (0x0).
Nov 20 '05 #4
Cor
Hi Cj,

Thanks,

Cor
Here ya go
Replace: 31 **item1**item2**item3

String.Replace: 16 **item1**item2**item3

Stringbuilder.Replace: 15 **item1**item2**item3

Split: 63 **item1**item2**item3

Regex.Replace: 266 **item1**item2**item3

----------------- now with longstring -------------

Replace: 1250 **item1**item2**item

String.Replace: 16 **item1**item2**item

Stringbuilder.Replace: 15 **item1**item2**item

Split: 985 **item1**item2**item

Regex.Replace: 109 **item1**item2**item

The program '[5092] speedtest1.exe' has exited with code 0 (0x0).

Nov 20 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: exBK | last post by:
I need to replace certain words in a string as below: strTitle="Microsoft test title for project ABC"; strKeywords="title project"; what I want to do is: "Bold" the keywords "title" and...
2
by: José Joye | last post by:
Hello, I was wondering if there is a method that exists to replace multi-spaces within a string with single-space. eg: "12 3 4 56" --> "12 3 4 56" I think this could be done by...
3
by: Craig Buchanan | last post by:
Is there a way to combine these two Replace into a single line? Regex.Replace(Subject, "\&", "&amp;") Regex.Replace(Subject, "\'", "&apos;") Perhaps Regex.Replace(Subject, "{\&|\'}", "{&amp;|&apos;}")...
4
by: Derek Martin | last post by:
I have an object with several string elements that I would like to check for invalid characters in the properties of each element. Can I use string.replace to do that or is there a better...
5
by: djc | last post by:
I need to prepare a large text database field to display in an asp.net repeater control. Currently I am replacing all chr(13)'s with a "<br/>" and it works fine. However, now I also want to be able...
3
by: gregpinero | last post by:
Hi guys, What I'm trying to do is find all instances of an acronymn such as IBM on a webpage and replace it with <acronym title="International Business Machines">IBM</acronym>. However in my...
13
by: CK | last post by:
Hi all, I have a textarea control. I am putting it's value in an html email. The problem is that the new lines are being ignored. I want to take the controls value and replace any newline...
10
by: pamelafluente | last post by:
I need to replace all the occurences of a string within another string (or stringbuilder): Function ReplaceInsensitive(ByVal InputString As String, _ ByVal SubstringReplaced As String, _ ByVal...
3
by: Pascal | last post by:
bonjour hello I would like to trim a string of all its white spaces so i used myString.trim() but it doesn't work as supposed : unsecable space are remaining in the middle of my string... i...
0
by: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
How does React native implement an English player?
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.