December 04, 2020, 10:53:55 AM

Author Topic: [bb] Again a new Replace command by PowerPC603 [ 1+ years ago ]  (Read 512 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
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
  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. CaseSens% = Input$("Case-sensitive (enter 0 or 1): ")
  8.  
  9. Print NewReplace$(StringToSearch$, StringToFind$, StringToReplace$, NumOfOccurances%, CaseSens%)
  10.  
  11. WaitKey()
  12.  
  13.  
  14.  
  15. Function NewReplace$(SourceString$, OldString$, NewString$, NumOfOcc% = 0, CaseSensitive% = True)
  16.         Local TargetString$
  17.  
  18.         Local Len_Source% = Len(SourceString$)
  19.         Local Len_Old% = Len(OldString$)
  20.  
  21.         If NumOfOcc% = 0 Then
  22.                 ; If user stated "0" occurances, replace them all
  23.                 Return Replace$(SourceString$, OldString$, NewString$)
  24.         Else
  25.                 ; Scan the entire string for each occurance
  26.                 For i = 1 To Len_Source%
  27.                         If CaseSensitive% = False Then
  28.                                 ; If stated NOT "Case-sensitive"
  29.                                 ; Convert the PartToSearch$ to LowerCase
  30.                                 PartOfSource$ = Lower$(Mid$(SourceString$ , i, Len_Old%))
  31.                                 ; Convert the Find$-string to LowerCase
  32.                                 OldString$ = Lower$(OldString$)
  33.                         Else
  34.                                 ; If stated "Case-sensitive", keep things original (don't convert to LowerCase)
  35.                                 PartOfSource$ = Mid$(SourceString$ , i, Len_Old%)
  36.                         EndIf
  37.  
  38.                         If (PartOfSource$ = OldString$) And (NumOfOcc% > 0) Then
  39.                                 ; If the occurance has been found and the number of occurances > 0,
  40.                                 ; Copy the "NewString$"
  41.                                 TargetString$ = TargetString$ + NewString$
  42.                                 ; Skip the number of chars of the "OldString$"
  43.                                 i = i + Len_Old% - 1
  44.                                 ; Decrease the remaining number of occurances to replace by 1
  45.                                 NumOfOcc% = NumOfOcc% - 1
  46.                         Else
  47.                                 ; If the occurance hasn't been found, copy the current character
  48.                                 TargetString$ = TargetString$ + Mid$(SourceString$, i, 1)
  49.                         EndIf
  50.                 Next
  51.         EndIf
  52.  
  53.         ; Return the "TargetString$"
  54.         Return TargetString$
  55. End Function


Comments : none...

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal