Just starting out? Need help? Post your questions and find answers here.
-
ZX80
- Enthusiast

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

- Posts: 452
- Joined: Sat Oct 21, 2023 4:06 pm
- Location: Hungary
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

- Posts: 364
- Joined: Mon Dec 12, 2016 1:37 pm
Post
by ZX80 »
miso, thanks for your version. It works !
-
Kiffi
- Addict

- Posts: 1491
- Joined: Tue Mar 02, 2004 1:20 pm
- Location: Amphibios 9
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

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

- Posts: 4155
- Joined: Thu Apr 18, 2019 8:17 am
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
-
jacdelad
- Addict

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

- Posts: 364
- Joined: Mon Dec 12, 2016 1:37 pm
Post
by ZX80 »
Thanks everyone !
The question is closed.
Kiffi, jacdelad, I thought so too (reverse the replacement sequence).
Thanks again to everyone !
-
NicTheQuick
- Addict

- Posts: 1510
- Joined: Sun Jun 22, 2003 7:43 pm
- Location: Germany, Saarbrücken
-
Contact:
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.
-
mk-soft
- Always Here

- Posts: 6224
- Joined: Fri May 12, 2006 6:51 pm
- Location: Germany
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