January 15, 2021, 06:03:26 PM

Author Topic: [bb] Alternate Replace command (2nd version) by PowerPC603 [ 1+ years ago ]  (Read 459 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
Title : Alternate Replace command (2nd version)
Author : PowerPC603
Posted : 1+ years ago

Description : The previous version had one flaw: it searched for an occurance of the "Find$"-string, replaced it with the "Replace$"-string
and started searching again on the new string (with the replacement).

Some results were strange:
SourceString$ = "This is a string where "is" must be replaced"
FindString$ = "is"
ReplaceString$ = "his"
NumOfOcc% = 2

Result: "Thhhis is a string where "is" must be replaced"

The routine replace the first "is" with "his" -> "Thhis is a string where "is" must be replaced"
Then it restarted from the start, where it would again find "is" in the first word "Thhis" -> "Thhhis is a string where "is" must be replaced"

So, in short, these strange results were only noticable when the "FindString$" ( = "is") is a part of the "ReplaceString$" (= "his").

This new version finds the positions of the occurances in the "SourceString$" before replacing them.
When all occurances have been found, then the function recreates the entire string and replaces each occurance (as many as stated by the NumOfOcc-parameter).
There's also a built-in limit-protection, so you cannot state more occurances than there are present in the string.

If you take the above example, then you see that there are only 3 occurances of the "FindString$" ("is").
If you call the routine like this:
Print NewReplace$(SourceString$, FindString$, ReplaceString$, 20)

Then the routine automatically limits the NumOfOcc% internally to 3.


Code :
Code: BlitzBasic
  1. Graphics 800, 600, 0, 2
  2.  
  3. StringToSearch$ = Input$("Enter Source-String: ")
  4. StringToFind$ = Input$("Enter Find$-string: ")
  5. StringToReplace$ = Input$("Enter Replace$-string: ")
  6. NumOfOccurances% = Input$("Enter NumOfOcc%: ")
  7.  
  8. Print NewReplace$(StringToSearch$, StringToFind$, StringToReplace$, NumOfOccurances%)
  9.  
  10. WaitKey()
  11.  
  12.  
  13.  
  14. Dim OccPos%(0)
  15. Function NewReplace$(SourceString$, OldString$, NewString$, NumOfOcc% = 0)
  16.         Local TotalOcc%
  17.         Local TargetString$
  18.  
  19.         Local Len_Source% = Len(SourceString$)
  20.         Local Len_Old% = Len(OldString$)
  21.  
  22.         ; Scan the entire string for each occurance and count them
  23.         For i = 1 To ((Len_Source% - Len_Old%) + 1)
  24.                 If Mid$(SourceString$, i, Len_Old%) = OldString$ Then
  25.                         TotalOcc% = TotalOcc% + 1
  26.                 EndIf
  27.         Next
  28.  
  29.         ; Set maximum occurances (in case user stated more than present)
  30.         If NumOfOcc% > TotalOcc% Then NumOfOcc% = TotalOcc%
  31.  
  32.         ; Redim the array to hold the positions of all required occurances
  33.         Dim OccPos(NumOfOcc%)
  34.  
  35.         ; Create the new "TargetString$"
  36.         If NumOfOcc% = 0 Then
  37.                 Return Replace$(SourceString$, OldString$, NewString$)
  38.         Else
  39.                 ; Find the positions of all occurances of the "OldString$" in the "SourceString$"
  40.                 For i = 1 To NumOfOcc%
  41.                         ; Find the position of each occurance
  42.                         If i = 1 Then
  43.                                 OccPos(i) = Instr(SourceString$, OldString$)
  44.                         Else
  45.                                 OccPos(i) = Instr(SourceString$, OldString$, OccPos(i-1) + Len_Old%)
  46.                         EndIf
  47.                 Next
  48.  
  49.                 ; Create the new string
  50.                 ; Copy the part before the first occurance to the "TargetString$"
  51.                 TargetString$ = Mid$(SourceString$, 1, OccPos(1) - 1)
  52.  
  53.                 ; Copy the "NewString$" for all but one of the occurances and the chars between each occurance
  54.                 For i = 1 To NumOfOcc% - 1
  55.                         If OccPos(i) <> 0 Then
  56.                                 TargetString$ = TargetString$ + NewString$
  57.                                 TargetString$ = TargetString$ + Mid$(SourceString$, OccPos(i) + Len_Old%, OccPos(i + 1) - (OccPos(i) + Len_Old%))
  58.                         EndIf
  59.                 Next
  60.  
  61.                 ; Copy the "NewString$" for the last occurance and the remaining part of the "SourceString$"
  62.                 If OccPos(NumOfOcc%) <> 0 Then
  63.                         TargetString$ = TargetString$ + NewString$
  64.                         TargetString$ = TargetString$ + Mid$(SourceString$, OccPos(NumOfOcc%) + Len_Old%)
  65.                 EndIf
  66.  
  67.                 Return TargetString$
  68.         EndIf
  69. End Function


Comments : none...

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal