Page 1 of 1

Is this a ReplaceString Bug?

Posted: Thu Jul 31, 2025 2:39 pm
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"

Re: Is this a ReplaceString Bug?

Posted: Thu Jul 31, 2025 2:51 pm
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,

Re: Is this a ReplaceString Bug?

Posted: Thu Jul 31, 2025 2:55 pm
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,".

Re: Is this a ReplaceString Bug?

Posted: Thu Jul 31, 2025 2:59 pm
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.

Re: Is this a ReplaceString Bug?

Posted: Thu Jul 31, 2025 3:03 pm
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.

Re: Is this a ReplaceString Bug?

Posted: Thu Jul 31, 2025 3:13 pm
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.

Re: Is this a ReplaceString Bug?

Posted: Thu Jul 31, 2025 5:49 pm
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.

Re: Is this a ReplaceString Bug?

Posted: Thu Jul 31, 2025 9:56 pm
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

Re: Is this a ReplaceString Bug?

Posted: Fri Aug 01, 2025 2:55 pm
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

Re: Is this a ReplaceString Bug?

Posted: Sat Aug 02, 2025 8:51 am
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