Is this a ReplaceString Bug?

Just starting out? Need help? Post your questions and find answers here.
swhite
Enthusiast
Enthusiast
Posts: 794
Joined: Thu May 21, 2009 6:56 pm

Is this a ReplaceString Bug?

Post by swhite »

Hi

Is this a bug or not.

Code: Select all

lcTxt.s=",,,,,,,,"
lcTxt=ReplaceString(lcTxt,",,",",0,")
Debug lcTxt ; result ",0,,0,,0,,0," but expected ",0,0,0,0,0,0,0"
Simon White
dCipher Computing
Axolotl
Addict
Addict
Posts: 821
Joined: Wed Dec 31, 2008 3:36 pm

Re: Is this a ReplaceString Bug?

Post by Axolotl »

I would confirm the behavior....
Sometimes using the full arguments helps, but not here.
Only the while loop can do it.

Code: Select all

lcTxt.s=",,,,,,,,"
lcTxt=ReplaceString(lcTxt,",,",",0,")
Debug lcTxt ; result ",0,,0,,0,,0," but expected ",0,0,0,0,0,0,0"

; == again 
lcTxt.s=",,,,,,,,"
lcTxt=ReplaceString(lcTxt,",,",",0,", #PB_String_CaseSensitive, 1, 20) 
Debug lcTxt ; result ",0,,0,,0,,0," but expected ",0,0,0,0,0,0,0"

; == again 
lcTxt.s=",,,,,,,,"
While FindString(lcTxt, ",,") 
  lcTxt=ReplaceString(lcTxt,",,",",0,") 
Wend   
Debug lcTxt ; result ",0,,0,,0,,0," but expected ",0,0,0,0,0,0,0"
Debug Output:

Code: Select all

,0,,0,,0,,0,
,0,,0,,0,,0,
,0,0,0,0,0,0,0,
Just because it worked doesn't mean it works.
PureBasic 6.04 (x86) and <latest stable version and current alpha/beta> (x64) on Windows 11 Home. Now started with Linux (VM: Ubuntu 22.04).
Little John
Addict
Addict
Posts: 4780
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Is this a ReplaceString Bug?

Post by Little John »

swhite wrote:Is this a ReplaceString Bug?
No.
'lcTxt' contains ",," 4 times, and each occurence of ",," is replaced with ",0,". So of course the result is ",0,,0,,0,,0,".
User avatar
Demivec
Addict
Addict
Posts: 4265
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Is this a ReplaceString Bug?

Post by Demivec »

I do not think it is a bug. The search and replace is started at the beginning of the source and moves character by character. It does not search the replacements after they have each been made.
Axolotl
Addict
Addict
Posts: 821
Joined: Wed Dec 31, 2008 3:36 pm

Re: Is this a ReplaceString Bug?

Post by Axolotl »

The experts' explanations make sense to me.
So you need the while loop (see above) for your expected result.

BTW: Maybe it's not so good to use the word bug in subject lines. Actually it's an observed behavior, the decision bug or feature will be made by Fred at the end of all days anyway.
Just because it worked doesn't mean it works.
PureBasic 6.04 (x86) and <latest stable version and current alpha/beta> (x64) on Windows 11 Home. Now started with Linux (VM: Ubuntu 22.04).
Quin
Addict
Addict
Posts: 1132
Joined: Thu Mar 31, 2022 7:03 pm
Location: Colorado, United States
Contact:

Re: Is this a ReplaceString Bug?

Post by Quin »

Axolotl wrote: Thu Jul 31, 2025 3:03 pm BTW: Maybe it's not so good to use the word bug in subject lines. Actually it's an observed behavior, the decision bug or feature will be made by Fred at the end of all days anyway.
He didn't call it a bug, he asked if it was a bug. Seems reasonable to me.
Little John
Addict
Addict
Posts: 4780
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Is this a ReplaceString Bug?

Post by Little John »

Demivec wrote: Thu Jul 31, 2025 2:59 pm It does not search the replacements after they have each been made.
Yep! And I thank heaven for that! Otherwise, that function would be a whole mess, and could even produce an endless loop.
BarryG
Addict
Addict
Posts: 4160
Joined: Thu Apr 18, 2019 8:17 am

Re: Is this a ReplaceString Bug?

Post by BarryG »

You probably want to do it like this (looks right, but do your own tests):

Code: Select all

lcTxt.s=",,,,,,,,"
lcTxt=ReplaceString(lcTxt,",",",0")
Debug lcTxt ; Result: ,0,0,0,0,0,0,0,0
swhite
Enthusiast
Enthusiast
Posts: 794
Joined: Thu May 21, 2009 6:56 pm

Re: Is this a ReplaceString Bug?

Post by swhite »

Hi

Thanks for all the responses.

I did realize that the solution was to use "while". It is not a problem if this is the expected behavior. However, it just caught me off guard in that I expected the search to continue from the last character replaced. I also cannot use BarryG's solution because the actual string is more like "'test',,345,'No',,," as it is part of an SQL statement so I do not want to replace every comma with ",0". I just gave my example to indicate the behavior and what I had expected.

Simon
Simon White
dCipher Computing
User avatar
STARGÅTE
Addict
Addict
Posts: 2228
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: Is this a ReplaceString Bug?

Post by STARGÅTE »

A regular expression can help here:
The expression ",(?=,)|,$" search for a "," which is followed by a other "," (?=,) or the end of the string ($):

Code: Select all

Define String.s = "'test',,345,'No',,," 

Define Regex.i = CreateRegularExpression(#PB_Any, ",(?=,)|,$")

Define NewString.s = ReplaceRegularExpression(Regex, String, ",0")

Debug String
Debug NewString
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
Post Reply