Ooops
November 28, 2020, 10:57:40 AM

Author Topic: [bb] New Replace command by PowerPC603 [ 1+ years ago ]  (Read 424 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] New Replace command by PowerPC603 [ 1+ years ago ]
« on: June 29, 2017, 12:28:41 AM »
Title : New Replace command
Author : PowerPC603
Posted : 1+ years ago

Description : The default Replace-command replaces all occurances of the Find$-string.
This new function gives you the ability to replace only a certain number of occurances in the given string.

Syntax: NewReplace$(Source$, Find$, Replace$, NumOfOcc% = 0)
NumOfOcc% is optional.

If NumOfOcc% = 0 -> replace all occurances of Find$ by Replace$.
If NumOfOcc% > 0 -> replace the first NumOfOcc% occurances of Find$ by Replace$.


Code :
Code: BlitzBasic
  1. Graphics 800, 600, 0, 2
  2.  
  3. StringToSearch$ = "This is a line where " + Chr(34) + "is" + Chr(34) + " must be replaced"
  4. StringToFind$ = "is"
  5. StringToReplace$ = "was"
  6. NumOfOccurances% = 1
  7.  
  8. Print StringToSearch$
  9. Print StringToFind$
  10. Print StringToReplace$
  11. Print NumOfOccurances%
  12. Print
  13.  
  14. If NumOfOccurances% > 0 Then
  15.         Print NewReplace$(StringToSearch$, StringToFind$, StringToReplace$, NumOfOccurances%)
  16. Else
  17.         Print NewReplace$(StringToSearch$, StringToFind$, StringToReplace$)
  18. EndIf
  19.  
  20. WaitKey()
  21.  
  22.  
  23.  
  24. Function NewReplace$(SourceString$, OldString$, NewString$, NumOfOcc% = 0)
  25.         Local TargetString$
  26.         Local FindOccPos%
  27.  
  28.         Local Len_Old% = Len(OldString$)
  29.  
  30.         If NumOfOcc% = 0 Then
  31.                 ; If number of occurances = 0, replace all occurances of "OldString$" with "NewString$"
  32.                 Return Replace$(SourceString$, OldString$, NewString$)
  33.         Else
  34.                 ; Proces the string as many times as stated by "NumOfOcc%"
  35.                 For i = 1 To NumOfOcc%
  36.                         ; Find the first occurance of the "OldString$"
  37.                         FindOccPos% = Instr(SourceString$, OldString$)
  38.                         ; If it's found, proces it
  39.                         If FindOccPos% > 0 Then
  40.                                 ; Copy the first part (before the "OldString$") to the "TargetString$"
  41.                                 TargetString$ = Mid$(SourceString$, 1, FindOccPos% - 1)
  42.                                 ; Copy the "NewString$" to the end of "TargetString$"
  43.                                 TargetString$ = TargetString$ + NewString$
  44.                                 ; Copy the rest of the "SourceString$" to the "TargetString$"
  45.                                 TargetString$ = TargetString$ + Mid$(SourceString$, FindOccPos% + Len_Old%)
  46.  
  47.                                 ; Replace the "SourceString$" by the newly created "TargetString$"
  48.                                 ; to begin reprocessing again (if needed = if NumOfOcc% > 1)
  49.                                 SourceString$ = TargetString$
  50.                         EndIf
  51.                 Next
  52.  
  53.                 ; Return the new "SourceString$"
  54.                 Return SourceString$
  55.         EndIf
  56. End Function


Comments : none...

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal