Replace Substring Within a String

Category: Strings

Date: 02-16-2022

Return to Index


 
'PowerBASIC has multiple functions which do string replacements. The
'REPLACE and MID$ functions are the two primary functions. REPLACE
'has the advantage of being able to change the length of the resulting string.
'However, EXTRACT$, REMAIN$, RETAIN$, and REMOVE$ are also useful.
 
'Primary Code:
'Syntax:   REPLACE [ANY] MatchString WITH NewString IN MainString   'case-sensitive
'Syntax:   MID$(string_var, start& [, length&]) = replacement
 
'REPLACE
'Syntax:  REPLACE [ANY] MatchString WITH NewString IN MainString
'Find and replace all occurrences of one string with another
'CAN change the length of the string
'IS case-sensitive
 
'Example#1 - replace string with a string
Dim a$
a$ = "on and off and ON"
Replace "onWith "offagainIN a$   'returns "offagain and off and ON", replaces all occurrences, longer result
'result is to replace all occurrences of "on" with "off"
'note that "ON" was not replaced because REPLACe is case-sensitive
'to replace a single occurrence of a sub-string, use MID$
 
'Example#2 - replace individual characters
Dim a$
a$ = "PowerBASIC"
Replace ANY "wSbWith "123IN a$   'returns "Po1erBA2IC"
'result is to replace w with 1, S with 2, b with 3
'note that B was not replaced because REPLACE is case-sensitive
 
 
'MID$
'Syntax:  MID$(string_var, start& [, length&]) = replacement
'Replace characters at specific location in a string
'CANNOT change the length of the string
 
'Example#3 - simply replace 3-characters at specified position
Dim a$
a$ = "123456"
Mid$(a$,2,3) = "abc"  'replace positions 2-4 with "abc", returns 1abc56"
 
'Example#4 - replace 1st occurrence of a substring
Dim a$, i as Long
a$ = "on and off and ON"
i = Instr(a$, "on")
Mid$(a$, i, Len("on")) = "XX"    'returns "XX and off and ON"
'replaces only the first occurrence of "on"
 
'Consider these other functions which provide string modifications:
'EXTRACT$ - Return string to left of first occurrence of a specified char or string
'REMAIN$ - Return string to right of first occurrence of a specified char or string
 
'RETAIN$ - Return only specified characters of a string
'REMOVE$ - Return string with specified characters removed
 
 
'Compilable Example:  (Jose Includes)
#Compiler PBWin 9, PBWin 10
#Compile EXE
#Dim All
%Unicode=1
#Include "Win32API.inc"
Global hDlg As Dword
 
Function PBMain() As Long
   Dialog New Pixels, 0, "Test Code",300,300,200,200, %WS_OverlappedWindow To hDlg
   Control Add Button, hDlg, 100,"Replace", 50,10,100,20
   Control Add Button, hDlg, 200,"Mid$", 50,40,100,20
   Dialog Show Modal hDlg Call DlgProc
End Function
 
CallBack Function DlgProc() As Long
   Local a$, i as Long
   If CB.Msg = %WM_Command AND CB.Ctl = 100 AND CB.Ctlmsg = %BN_Clicked Then
      a$ = "on and off and ON"
      Replace "onWith "offagainIN a$   'returns "offagain and off and ON", replaces all occurrences, longer result
      MsgBox a$
      a$ = "PowerBASIC"
      Replace ANY "wSbWith "123IN a$   'returns "Po1erBA2IC"
      MsgBox a$
   End If
   If CB.Msg = %WM_Command AND CB.Ctl = 200 AND CB.Ctlmsg = %BN_Clicked Then
      a$ = "123456"
      Mid$(a$,2,3) = "abc"  'replace positions 2-4 with "abc", returns 1abc56"
      MsgBox a$
      a$ = "on and off and ON"
      i = Instr(a$, "on")
      Mid$(a$, i, Len("on")) = "XX"    'returns "XX and off and ON"
      MsgBox a$
   End If
End Function
 
'gbs_00243
'Date: 03-10-2012


created by gbSnippets
http://www.garybeene.com/sw/gbsnippets.htm