[SOLVED] confusion with ReplaceString()

Just starting out? Need help? Post your questions and find answers here.
ZX80
Enthusiast
Enthusiast
Posts: 364
Joined: Mon Dec 12, 2016 1:37 pm

[SOLVED] confusion with ReplaceString()

Post 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.
Last edited by ZX80 on Sat Aug 09, 2025 11:04 am, edited 1 time in total.
miso
Enthusiast
Enthusiast
Posts: 452
Joined: Sat Oct 21, 2023 4:06 pm
Location: Hungary

Re: confusion with ReplaceString()

Post 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
ZX80
Enthusiast
Enthusiast
Posts: 364
Joined: Mon Dec 12, 2016 1:37 pm

Re: confusion with ReplaceString()

Post by ZX80 »

miso, thanks for your version. It works !
User avatar
Kiffi
Addict
Addict
Posts: 1491
Joined: Tue Mar 02, 2004 1:20 pm
Location: Amphibios 9

Re: confusion with ReplaceString()

Post 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
?
Hygge
normeus
Enthusiast
Enthusiast
Posts: 472
Joined: Fri Apr 20, 2012 8:09 pm
Contact:

Re: confusion with ReplaceString()

Post 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
google Translate;Makes my jokes fall flat- Fait mes blagues tombent à plat- Machte meine Witze verpuffen- Eh cumpari ci vo sunari
BarryG
Addict
Addict
Posts: 4155
Joined: Thu Apr 18, 2019 8:17 am

Re: confusion with ReplaceString()

Post 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
User avatar
jacdelad
Addict
Addict
Posts: 1999
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Re: confusion with ReplaceString()

Post 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...
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
ZX80
Enthusiast
Enthusiast
Posts: 364
Joined: Mon Dec 12, 2016 1:37 pm

Re: confusion with ReplaceString()

Post by ZX80 »

Thanks everyone !
The question is closed.

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

Thanks again to everyone !
User avatar
NicTheQuick
Addict
Addict
Posts: 1510
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: [SOLVED] confusion with ReplaceString()

Post by NicTheQuick »

In such cases always start replacing the longest patterns first.
Last edited by NicTheQuick on Sat Aug 09, 2025 11:50 am, edited 1 time in total.
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
User avatar
mk-soft
Always Here
Always Here
Posts: 6224
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: [SOLVED] confusion with ReplaceString()

Post 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
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Post Reply