Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 16:34 05 Dec 2026 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : Does MMBasic have a REPLACE function (or sub)

Author Message
thwill

Guru

Joined: 16/09/2019
Location: United Kingdom
Posts: 4424
Posted: 10:36pm 24 Nov 2026
Copy link to clipboard 
Print this post

Hi folks,

Does MMBasic have a single function/sub that does string matching and replacement yet? - I'm not aware of anything up to v6 but I haven't been paying full attentions since then.

Thanks,

Tom
Edited 2026-11-25 08:37 by thwill
MMBasic for Linux, Game*Mite, CMM2 Welcome Tape, Creaky old text adventures
 
homa

Guru

Joined: 05/11/2021
Location: Germany
Posts: 790
Posted: 11:09pm 24 Nov 2026
Copy link to clipboard 
Print this post

Not exactly string, BUT
LONGSTRING REPLACE array%() , string$, start
Will substitute characters in the normal MMBasic string ‘string$’ into an
existing long string ‘array%()’ starting at position ‘start’ in the long string.

OR

Appendix E – Regex Syntax
Regex Syntax
The alternate forms of the INSTR() and LINSTR() functions can take a regular expression as the search pattern.
The alternate form of the functions are:
INSTR([start],text$, search$ [,size])
LINSTR(text%(),search$ [,start] [,size]
Edited 2026-11-25 09:11 by homa
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6647
Posted: 02:55am 25 Nov 2026
Copy link to clipboard 
Print this post

Have a play with INSTR and MID$

  Quote  INSTR( [start-position,]
string-searched$, stringpattern$ [,size] )
Returns the position at which 'string-pattern$' occurs in 'string-searched$',
beginning at 'start-position'. If 'start-position' is not provided it will default to 1.
Both the position returned and 'start-position' use 1 for the first character, 2 for
the second, etc.
The function returns zero if 'string-pattern$' is not found.
If the optional parameter “size” is specified the “string-pattern” is treated as a
regular expression. See Appendix E for the details.


  Quote  MID$( str$, start, num) = str2$
The ‘num’ characters in 'str$', beginning at position 'start', are replaced by the
characters in 'str2$'. ‘num’ can be set be 0 i.e. insert the new string at the
position specified. If num isn’t specified it defaults to the length of ‘str2$’


or you might prefer regex

Jim
VK7JH
MMedit
 
Turbo46

Guru

Joined: 24/12/2017
Location: Australia
Posts: 1695
Posted: 05:10am 25 Nov 2026
Copy link to clipboard 
Print this post

I found this on Fruit of the Shed.

Function Replace$(a$,b$,c$)
Local Integer z,d,e
Local r$,x$
z=1:d=Len(c$):e=Len(b$):r$=a$
Do
z=Instr(z,r$,b$)
If z=0 Then Exit Do
x$=Left$(r$,z-1)
r$=x$+c$+Mid$(r$,z+e)
z=Len(x$)+d
Loop
Replace$=r$
End Function

and asked Claude if it could improve it. The response:

There's a bug in the original, plus a couple of smaller issues.

The bug: after a replacement, z=Len(x$)+d points at the last character of the replacement, not the one after it. If the replacement ends with something that matches the start of the search string, it gets rescanned. Replace$("a","a","aa") loops forever, because each pass finds the "a" it just inserted. When c$ is empty, z can also become 0, which is a bad start position for Instr.

Other issues:

An empty b$ isn't guarded against.
It rebuilds the whole string on every match. Walking forward and appending is simpler and avoids the position arithmetic that caused the bug.

Function Replace$(a$,b$,c$)
Local Integer z,p,e
Local r$
e=Len(b$)
If e=0 Then Replace$=a$ : Exit Function
p=1
Do
z=Instr(p,a$,b$)
If z=0 Then Exit Do
If z>p Then r$=r$+Mid$(a$,p,z-p)
r$=r$+c$
p=z+e
Loop
Replace$=r$+Mid$(a$,p)
End Function


Because it only ever searches the original string a$, replacement text is never rescanned, so the infinite-loop case can't occur. The behaviour is unchanged otherwise.

One limitation remains: MMBasic strings max out at 255 characters, so if the replacement lengthens the string past that, you'll get a "String too long" error. If that's likely in your use, I can add a length check that truncates or returns the original.

My thought is that it is better to have it error out than silently either do nothing or truncate the result.

Bill
Keep safe. Live long and prosper.
 
twofingers

Guru

Joined: 02/06/2014
Location: Germany
Posts: 1786
Posted: 11:40am 25 Nov 2026
Copy link to clipboard 
Print this post

Hi Tom,
if it’s just a matter of performing replacements within text files, the latest version of the original FM (Javavis) might be of help. But I assume you’re simply looking for the relevant function ...?  
Regards
Michael
 
thwill

Guru

Joined: 16/09/2019
Location: United Kingdom
Posts: 4424
Posted: 12:16pm 25 Nov 2026
Copy link to clipboard 
Print this post

Thanks folks, I was already using an INSTR + MID$ version, I was just wondering if amongst all the new functionality there was a single-call implementation.

My adventure game (still in progress) performs a lot of string replacement to canonicalise the player's input before matching against the suspect's dialogue patterns.

No worries, I've got over it.

Thanks,

Tom
MMBasic for Linux, Game*Mite, CMM2 Welcome Tape, Creaky old text adventures
 
Print this page


To reply to this topic, you need to log in.

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2026