User Tools

Site Tools


mmbasic:midd_function_to_replace_a_section_of_a_string_works_like_vb_statement_mid

Midd$ Function to replace a section of a string, works like VB statement Mid$()

String slicing (extracting portions of a string) is key functionality and most languages employ some form of Left$, Mid$, Right$ to do so - even the humble ZX81, although not using specific functions provided surprisingly powerful slicing.

Mid$ can be used to extract any part of a string, indeed, Left$() & Right$() could even be consigned to the scrap heap and their functionality easily assumed by Mid$(). Most are familiar with this use as a function:

x$=Mid$(a$,3,4)

Microsoft Basics from GWBasic onwards also implement Mid$() as a statement to replace a section of a string

Mid$(x$,3[,n])=a$

This latter, statement form, although absent in MMBasic is fairly easily reproduced and the Function shown below does just this. It is not entirely faithful as imposition is not supported in MMBasic (<statement>=<argument>). Also, the name has to be slightly different to avoid syntax errors etc due to

Mid 

being a reserved word.

Syntax:

  =midd$(string,position,string)

Examples:

  a$=midd$(x$,4,t$)
  Print midd$("Trevor,3,"FRED")

Code:

    'Function to reproduce Mid$(MyString$,position,substitute$)=
    Function Midd$(a$,p As Integer,s$)
        Midd$=Left$(a$,p-1)+s$+Mid$(a$,p+Len(s$))
    End Function

Example Output

> ?Midd$("Trevor",2,"R")
TRevor

> ?Midd$("Trevor",12,"FRED")
TrevorFRED

> ?midd$("trevor",3,"zz")
trzzor
mmbasic/midd_function_to_replace_a_section_of_a_string_works_like_vb_statement_mid.txt · Last modified: 2024/01/19 09:30 by 127.0.0.1