Code archives/Algorithms/Alternate Replace command (2nd version)
This code has been declared by its author to be Public Domain code.
Download source code
| |||||
| 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. | |||||
Graphics 800, 600, 0, 2
StringToSearch$ = Input$("Enter Source-String: ")
StringToFind$ = Input$("Enter Find$-string: ")
StringToReplace$ = Input$("Enter Replace$-string: ")
NumOfOccurances% = Input$("Enter NumOfOcc%: ")
Print NewReplace$(StringToSearch$, StringToFind$, StringToReplace$, NumOfOccurances%)
WaitKey()
Dim OccPos%(0)
Function NewReplace$(SourceString$, OldString$, NewString$, NumOfOcc% = 0)
Local TotalOcc%
Local TargetString$
Local Len_Source% = Len(SourceString$)
Local Len_Old% = Len(OldString$)
; Scan the entire string for each occurance and count them
For i = 1 To ((Len_Source% - Len_Old%) + 1)
If Mid$(SourceString$, i, Len_Old%) = OldString$ Then
TotalOcc% = TotalOcc% + 1
EndIf
Next
; Set maximum occurances (in case user stated more than present)
If NumOfOcc% > TotalOcc% Then NumOfOcc% = TotalOcc%
; Redim the array to hold the positions of all required occurances
Dim OccPos(NumOfOcc%)
; Create the new "TargetString$"
If NumOfOcc% = 0 Then
Return Replace$(SourceString$, OldString$, NewString$)
Else
; Find the positions of all occurances of the "OldString$" in the "SourceString$"
For i = 1 To NumOfOcc%
; Find the position of each occurance
If i = 1 Then
OccPos(i) = Instr(SourceString$, OldString$)
Else
OccPos(i) = Instr(SourceString$, OldString$, OccPos(i-1) + Len_Old%)
EndIf
Next
; Create the new string
; Copy the part before the first occurance to the "TargetString$"
TargetString$ = Mid$(SourceString$, 1, OccPos(1) - 1)
; Copy the "NewString$" for all but one of the occurances and the chars between each occurance
For i = 1 To NumOfOcc% - 1
If OccPos(i) <> 0 Then
TargetString$ = TargetString$ + NewString$
TargetString$ = TargetString$ + Mid$(SourceString$, OccPos(i) + Len_Old%, OccPos(i + 1) - (OccPos(i) + Len_Old%))
EndIf
Next
; Copy the "NewString$" for the last occurance and the remaining part of the "SourceString$"
If OccPos(NumOfOcc%) <> 0 Then
TargetString$ = TargetString$ + NewString$
TargetString$ = TargetString$ + Mid$(SourceString$, OccPos(NumOfOcc%) + Len_Old%)
EndIf
Return TargetString$
EndIf
End Function |
Comments
None.
Code Archives Forum