Replace$
Result$ = Replace$(SourceString$, FindString$, ReplaceString$, [StartPos=1], [CaseFlag=1], [FirstFlag=0])
 
Parameters:

    SourceString$ = The String you wish to search and replace words/characters in
    FindString$ = The String of characters you wish to find in the search string
    ReplaceString$ = The characters you wish to insert in FindStrings place
    [StartPos=1] = The character to start searching in Source String at
    [CaseFlag=1]= Use case insensitive matching (0=no, 1= yes)
    [FirstFlag=0] = Replace only the first occurrence ? (0 = no, 1=yes)
Returns:

    Result$ = The Source String after the replacements have been made
 

      The Replace$() function will scan through a string and remove/replace words or groups of characters with another one. Replace$ often used to filter strings and remove any unwanted characters or words from them.

      The command lets you controls over where to start searching in the source string, as well as how it should perform the matching and even if it should replace every occurrence or just the first. Allowing you to manipulate the string with greater control and flexibility.



FACTS:


      * Use Instring() to search for a words or characters wiuthin a string.



Mini Tutorial #1:


      This example uses the replace$ function to change words and characters within a string.

  
  
; This will Replace the "Basic" word in the string
; "Play Basic" with the word "Games"..
  Print Replace$("Play Basic","Basic","Games",1,0,0)
  
; This will Replace the letter "a" in the string
; "Play Basic" with the character "-"..
  Print Replace$("Play Basic","a","-",1,0,0)
  
; This will insert the string "<<>>" in the place
; of the " " character
  Print Replace$("Play Basic"," ","<<>>",1,0,0)
  
; This will insert the string "-" in the place
; of the first "a" character
  Print Replace$("Play Basic","a","-",1,0,1)
  
; This will insert the string "-" in the place
; of the second "a" character
  Print Replace$("Play Basic","a","-",4,0,1)
  
  
; Display the Screen and wait for the user to press a key
  Sync
  WaitKey
  




This example would output.

  
  Play Games
  Pl-y B-sic
  Play<<>>Basic
  Pl-y Basic
  Play B-sic
  





Mini Tutorial #2:


      This example loads a ASCII text file into a string array. The replace$ function is used here to removed unwanted EndOfLine characters from the ASCII text, which is then passed to the SplitToArray function.


  
; Try and locate the index page frim the help files
; for something to load and view
  TextFileName$=ProgramDir$()
  TextFilename$+="\Help\Commands\Index.htm"
  
  
  Dim TextFile$(0)
  
  LineCount=LoadTextFileToArray(TextFile$(), TextFilename$)
  
  Print "File:"+TextFilename$
  Print "Lines:"+Str$(LineCount)
  Print ""
  
  For lp=0 To LineCount
     Print "line#"+Digits$(lp,4)+"  "+TextFile$(lp)
  Next
  
  
  Sync
  WaitKey
  
  
  
  
Function LoadTextFileToArray(ThisArray$(), TextFilename$)
  
  // check if the file exists ??
  If FileExist( TextFilename$)
     
     // get the size of the file in bytes
     Size=FileSize(TextFilename$)
     If Size
        
        // Open a new file handle
        fh=ReadNewFile(TextFileName$)
        If fh
           // read the entire file in as a string
           s$=ReadChr$(fh,Size)
           
           // close the file handle as we're done with it
           CloseFile Fh
           
           // Strip the end of line characters (if any)
           s$=Replace$(s$,Chr$(13),"")
           
           // Split the string into rows and store in our array
           LIneCount=SplitToArray(s$,Chr$(10),ThisArray$(),7)
           
        EndIf
        
     EndIf
     
  EndIf
  
  If LineCount<1
     Dim ThisArray$(0)
  EndIf
  
EndFunction LineCount
  
  


 
Related Info: Insert$ | Instring | Make$ | Pad$ | SplitToArray :
 


(c) Copyright 2002 - 2024 - Kevin Picone - PlayBASIC.com