| StringBuilder.Replace(str, str) ; BUG? (.NET 1.1) |
|
| Author |
Message |
CJW99

|
Posted: Visual C# Language, StringBuilder.Replace(str, str) ; BUG? (.NET 1.1) |
Top |
Hi All,
I have the following string:
"ST ST ST ST ST ST ST ST ST"
I put this in a StringBuilder. Then I call Replace(" ST ", "SAINT"). Please not the 1 preceding and 1 trailing space in the string " ST "
My output is:
ST SAINT ST SAINT ST SAINT ST SAINT ST
When I would expect:
ST SAINT SAINT SAINT SAINT SAINT SAINT SAINT ST.
Why is every occurrence of " ST " not being replaced A 'workaround' would be to call the replace method twice... BUT I think that StringBuilder is not performaing as it should.
I am Using .NET 1.1. Thanks everyone.
Chris
Visual C#4
|
| |
|
| |
 |
SvenC

|
Posted: Visual C# Language, StringBuilder.Replace(str, str) ; BUG? (.NET 1.1) |
Top |
It is replaces sequentially, so after " ST ST " has replaced the first " ST " you have "SAINTST ".
Use Replace(" ST ", " SAINT ")
-- SvenC
|
| |
|
| |
 |
CJW99

|
Posted: Visual C# Language, StringBuilder.Replace(str, str) ; BUG? (.NET 1.1) |
Top |
Hi All,
I have the following string:
"ST ST ST ST ST ST ST ST ST"
I put this in a StringBuilder. Then I call Replace(" ST ", " SAINT "). Please note the 1 preceding and 1 trailing space in the string " ST " AND the string " SAINT ".
My output is:
ST SAINT ST SAINT ST SAINT ST SAINT ST
When I would expect:
ST SAINT SAINT SAINT SAINT SAINT SAINT SAINT ST.
Why is every occurrence of " ST " not being replaced A 'workaround' would be to call the replace method twice... BUT I think that StringBuilder is not performing as it should.
I am Using .NET 1.1. Thanks everyone. If you dont believe me try it yourself!!!! AND if i'm worng you can bag me all you want!!
Chris
|
| |
|
| |
 |
Markku Behm

|
Posted: Visual C# Language, StringBuilder.Replace(str, str) ; BUG? (.NET 1.1) |
Top |
Hi Chris
Actually your string is like this: "ST" + " ST " + "ST" + " ST " + "ST" + " ST " + "ST" + " ST " + "ST
And replaces string like this: "ST" + " SAINT " + "ST" + " SAINT " + "ST" + " SAINT " + "ST" + " SAINT " + "ST"
Hope this help.
Markku
|
| |
|
| |
 |
RizwanSharp

|
Posted: Visual C# Language, StringBuilder.Replace(str, str) ; BUG? (.NET 1.1) |
Top |
Markku Behm wrote: | |
Hi Chris
Actually your string is like this: "ST" + " ST " + "ST" + " ST " + "ST" + " ST " + "ST" + " ST " + "ST
And replaces string like this: "ST" + " SAINT " + "ST" + " SAINT " + "ST" + " SAINT " + "ST" + " SAINT " + "ST"
Hope this help.
Markku
|
|
Its a cross post by CJW99 and already replied on some other forum. Markku's answers is absolutely right :).
Best Regards,
Rizwan
|
| |
|
| |
 |
ahmedilyas

|
Posted: Visual C# Language, StringBuilder.Replace(str, str) ; BUG? (.NET 1.1) |
Top |
merging threads. Please do not cross post/make multiple threads.
|
| |
|
| |
 |
CJW99

|
Posted: Visual C# Language, StringBuilder.Replace(str, str) ; BUG? (.NET 1.1) |
Top |
Thanks all,
Apologies but I went edit thread at aork at when I got home it wasn't edited, and to emphasise a small point that could easily be missed (that of the leading and trailing spaces), I decided to repost:
|
Hi Chris
Actually your string is like this: "ST" + " ST " + "ST" + " ST " + "ST" + " ST " + "ST" + " ST " + "ST
And replaces string like this: "ST" + " SAINT " + "ST" + " SAINT " + "ST" + " SAINT " + "ST" + " SAINT " + "ST"
Hope this help.
Markku
This actually does help a lot, and gives mr insight into how the sBuilder is doing its replace. But to me thats not a true replace method. Consider this:
replace all occurrences of sb with ZZ:
sbsbsbsbsbsbsb sbsbsb would become like ZZZZZZZZZZZZZZ ZZZZZZ
EVERY occurence of sb is replaced by ZZ! In by my case every case of the target string is not replaced. If it were to find and replace " ST " one at a time with " SAINT " it would pick up all instances of " ST " because leading and trailing spaces would be preserved. Is there a reason why it does not do this
THANK YOU VERY MUCH FOR THE INSIGHT !!!!! :)
Its much appreciated.
Chris
|
|
| |
|
| |
 |
Markku Behm

|
Posted: Visual C# Language, StringBuilder.Replace(str, str) ; BUG? (.NET 1.1) |
Top |
Hi
I'm not sure can I explaint that so that you understant (my english, you know) but I try.
When program start replace those substrings it find fist matching point start at 3th and end 6th char. So, it change that and continue.
Next matching point is 9th char at original string, not 6th because that was allready replaced.
Hope this explain what happend.
Markku
|
| |
|
| |
 |
Peter Ritchie

|
Posted: Visual C# Language, StringBuilder.Replace(str, str) ; BUG? (.NET 1.1) |
Top |
Try placing the spaces with 'b' to see why you're getting what you're getting:
"STbSTbSTbSTbSTbSTbSTbSTbST", Replace("bSTb", "SAINT")...
The Replace method finds the first one:
"STbSTbSTbSTbSTbSTbSTbSTbST": resulting in:
"STSAINTSTbSTbSTbSTbSTbSTbST". The next find is:
"STSAINTSTbSTbSTbSTbSTbSTbST", etc...
|
| |
|
| |
 |
CJW99

|
Posted: Visual C# Language, StringBuilder.Replace(str, str) ; BUG? (.NET 1.1) |
Top |
Hi Peter,
You forgot to replace the spaces in the saint string with b's too (i.e. "bSAINTb").
So to use the good way you explained things my scenario would look like:
"STbSTbSTbSTbSTbSTbSTbSTbST": resulting in:
"STbSAINTbSTbSTbSTbSTbSTbSTbST". The next find is:
"STbSAINTbSTbSTbSTbSTbSTbSTbST", etc...
It should get them all, but does not. The reason must be because it parses all instances of the substring first, and then replaces each instance it has found (without considering the implications of the replace). An algorithmic flaw or not The more I think of it probably not; as if we considered every replace possibility after a replace we could end up with an endless loop, growing string, and troubles!
Thanks all
Chris
|
| |
|
| |
 |
Peter Ritchie

|
Posted: Visual C# Language, StringBuilder.Replace(str, str) ; BUG? (.NET 1.1) |
Top |
Hi CJW99, your first post didn't include spaces around SAINT. But, if it did (switching from "SAINT" to "bSAINTb", what would occur would be this:
First found: STbSTbSTbSTbSTbSTbSTbSTb
First replace: STbSAINTbSTbSTbSTbSTbSTbSTb
Now at this point the next search will only be searching the green portion (it doesn't start again from the beginning): STbSAINTbSTbSTbSTbSTbSTbSTb
In which case the next find will be: STbSAINTbSTbSTbSTbSTbSTbSTb
Continuing on with the above results in: STbSAINTbSTbSAINTbSTbSAINTbSTbSAINTb
...which, if I remember correctly, the result you observed.
|
| |
|
| |
 |
CJW99

|
Posted: Visual C# Language, StringBuilder.Replace(str, str) ; BUG? (.NET 1.1) |
Top |
Aaaah yes there was confusion then my posts got merged etc.
Thanks for your help everyone, it was the result. All solved now :)
|
| |
|
| |
 |
| |
|