Page 1 of 1

[SOLVED] confusion with ReplaceString()

Posted: Thu Aug 07, 2025 7:36 pm
by ZX80
Hi, all.

Is it possible to explicitly specify to replace only the first word found and then exit ?

Code: Select all

Define string.s = "ABC time DEF GHIJK LM mtime NOP"
string = ReplaceString(string, "time", "2316")
Debug string
string = ReplaceString(string, "mtime", "082316")
Debug string
Otherwise it breaks all the rest of the logic.
Well or something like... search for the whole word.

Re: confusion with ReplaceString()

Posted: Thu Aug 07, 2025 7:40 pm
by miso
Is it possible to format the string? I mean, like this:

Code: Select all

Define string.s = "ABC %time DEF GHIJK LM %mtime NOP"
string = ReplaceString(string, "%time", "2316")
Debug string
string = ReplaceString(string, "%mtime", "082316")
Debug string

Re: confusion with ReplaceString()

Posted: Thu Aug 07, 2025 7:46 pm
by ZX80
miso, thanks for your version. It works !

Re: confusion with ReplaceString()

Posted: Thu Aug 07, 2025 8:41 pm
by Kiffi

Code: Select all

Define string.s = "ABC time DEF GHIJK LM mtime NOP"
string = ReplaceString(string, "mtime", "082316")
Debug string
string = ReplaceString(string, "time", "2316")
Debug string
?

Re: confusion with ReplaceString()

Posted: Thu Aug 07, 2025 8:53 pm
by normeus

Code: Select all

Define string.s = "ABC time DEF GHIJK LM mtime NOP"
string = ReplaceString(string, "time", "2316",#PB_String_NoCase,1,1)
Debug string
string = ReplaceString(string, "mtime", "082316")
Debug string
Norm

Re: confusion with ReplaceString()

Posted: Thu Aug 07, 2025 10:06 pm
by BarryG
ZX80 wrote: Thu Aug 07, 2025 7:36 pmIs it possible to explicitly specify to replace only the first word found and then exit ?
Yes, the help for ReplaceString() shows that it has a "number of occurrences" flag, so set it to "1" to exit after the first match:

Code: Select all

Define string.s = "ABC time DEF GHIJK LM mtime NOP"
string = ReplaceString(string, "time", "2316", #PB_String_CaseSensitive, 1, 1)
Debug string
string = ReplaceString(string, "mtime", "082316", #PB_String_CaseSensitive, 1, 1)
Debug string

Re: confusion with ReplaceString()

Posted: Fri Aug 08, 2025 8:33 am
by jacdelad
My two cents:

Given the string you provided and leaving out the occurrences parameter:

Code: Select all

Define string.s = "ABC time DEF GHIJK LM mtime NOP"
string = ReplaceString(string, " time", " 2316")
Debug string
string = ReplaceString(string, "mtime", "082316")
Debug string
Or simply replace mtime first before replacing time...

Re: confusion with ReplaceString()

Posted: Sat Aug 09, 2025 11:03 am
by ZX80
Thanks everyone !
The question is closed.

Kiffi, jacdelad, I thought so too (reverse the replacement sequence).

Thanks again to everyone !

Re: [SOLVED] confusion with ReplaceString()

Posted: Sat Aug 09, 2025 11:28 am
by NicTheQuick
In such cases always start replacing the longest patterns first.

Re: [SOLVED] confusion with ReplaceString()

Posted: Sat Aug 09, 2025 11:49 am
by mk-soft
Placeholders in a string should always have a unique identifier.

Code: Select all

Define string.s = "ABC %time% DEF GHIJK LM %mtime% NOP"
string = ReplaceString(string, "%mtime%", "082316")
Debug string
string = ReplaceString(string, " %time%", " 2316")
Debug string
Or with Format like sprintf ...

Code: Select all

; Format like sprintf
; Link: https://www.purebasic.fr/english/viewtopic.php?t=32026

IncludeFile "Format.pb"

Define result.s, var1, var2.s

var1 = 2316
var2 = "082316"
result = Format("ABC %i DEF GHIJK LM %s NOP", @var1, @var2)
Debug result