[bb] Again a new Replace command by PowerPC603 [ 1+ years ago ]

Started by BlitzBot, June 29, 2017, 00:28:41

Previous topic - Next topic

BlitzBot

Title : Again a new Replace command
Author : PowerPC603
Posted : 1+ years ago

Description : This new command can replace a certain number of occurances of the Find$-string, instead of ALL occurances (like the default Replace$-command).

It is also capable of doing a Case-sensitive search & replace.

Both the Number of occurances and the CaseSentitive parameters are optional.

And it doesn't use an array like the previous version did.
It loops through all characters in the SourceString$, checks if the next characters match the Find$-string (Case-sensitive or not) and replaces it, if the remaining NumOfOCC% is higher than 0.


Code :
Code (blitzbasic) Select
Graphics 800, 600, 0, 2

StringToSearch$ = Input$("Enter Source-String: ")
StringToFind$ = Input$("Enter Find$-string: ")
StringToReplace$ = Input$("Enter Replace$-string: ")
NumOfOccurances% = Input$("Enter NumOfOcc%: ")
CaseSens% = Input$("Case-sensitive (enter 0 or 1): ")

Print NewReplace$(StringToSearch$, StringToFind$, StringToReplace$, NumOfOccurances%, CaseSens%)

WaitKey()



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

Local Len_Source% = Len(SourceString$)
Local Len_Old% = Len(OldString$)

If NumOfOcc% = 0 Then
; If user stated "0" occurances, replace them all
Return Replace$(SourceString$, OldString$, NewString$)
Else
; Scan the entire string for each occurance
For i = 1 To Len_Source%
If CaseSensitive% = False Then
; If stated NOT "Case-sensitive"
; Convert the PartToSearch$ to LowerCase
PartOfSource$ = Lower$(Mid$(SourceString$ , i, Len_Old%))
; Convert the Find$-string to LowerCase
OldString$ = Lower$(OldString$)
Else
; If stated "Case-sensitive", keep things original (don't convert to LowerCase)
PartOfSource$ = Mid$(SourceString$ , i, Len_Old%)
EndIf

If (PartOfSource$ = OldString$) And (NumOfOcc% > 0) Then
; If the occurance has been found and the number of occurances > 0,
; Copy the "NewString$"
TargetString$ = TargetString$ + NewString$
; Skip the number of chars of the "OldString$"
i = i + Len_Old% - 1
; Decrease the remaining number of occurances to replace by 1
NumOfOcc% = NumOfOcc% - 1
Else
; If the occurance hasn't been found, copy the current character
TargetString$ = TargetString$ + Mid$(SourceString$, i, 1)
EndIf
Next
EndIf

; Return the "TargetString$"
Return TargetString$
End Function


Comments : none...