Code archives/Algorithms/New Replace command

This code has been declared by its author to be Public Domain code.

Download source code

New Replace command by PowerPC603
(Posted 22 years ago)
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$.
Graphics 800, 600, 0, 2

StringToSearch$ = "This is a line where " + Chr(34) + "is" + Chr(34) + " must be replaced"
StringToFind$ = "is"
StringToReplace$ = "was"
NumOfOccurances% = 1

Print StringToSearch$
Print StringToFind$
Print StringToReplace$
Print NumOfOccurances%
Print

If NumOfOccurances% > 0 Then
	Print NewReplace$(StringToSearch$, StringToFind$, StringToReplace$, NumOfOccurances%)
Else
	Print NewReplace$(StringToSearch$, StringToFind$, StringToReplace$)
EndIf

WaitKey()



Function NewReplace$(SourceString$, OldString$, NewString$, NumOfOcc% = 0)
	Local TargetString$
	Local FindOccPos%

	Local Len_Old% = Len(OldString$)

	If NumOfOcc% = 0 Then
		; If number of occurances = 0, replace all occurances of "OldString$" with "NewString$"
		Return Replace$(SourceString$, OldString$, NewString$)
	Else
		; Proces the string as many times as stated by "NumOfOcc%"
		For i = 1 To NumOfOcc%
			; Find the first occurance of the "OldString$"
			FindOccPos% = Instr(SourceString$, OldString$)
			; If it's found, proces it
			If FindOccPos% > 0 Then
				; Copy the first part (before the "OldString$") to the "TargetString$"
				TargetString$ = Mid$(SourceString$, 1, FindOccPos% - 1)
				; Copy the "NewString$" to the end of "TargetString$"
				TargetString$ = TargetString$ + NewString$
				; Copy the rest of the "SourceString$" to the "TargetString$"
				TargetString$ = TargetString$ + Mid$(SourceString$, FindOccPos% + Len_Old%)

				; Replace the "SourceString$" by the newly created "TargetString$"
				; to begin reprocessing again (if needed = if NumOfOcc% > 1)
				SourceString$ = TargetString$
			EndIf
		Next

		; Return the new "SourceString$"
		Return SourceString$
	EndIf
End Function

Comments

None.

Code Archives Forum