Advertisement
C_Volume2 String Manipulation #79055

CReplaceString

CReplaceString is a function that I decided to code when I was bored. Had no idea what to code so I decided to code an advanced ReplaceString function. The concept is simple, it will replace a specified text in the string with something else.

AI

สรุปโดย AI: This codebase represents a historical implementation of the logic described in the metadata. Our preservation engine analyzes the structure to provide context for modern developers.

ซอร์สโค้ด
original-source
Public Function CReplaceString(ToFind As String, ReplaceWith As String, ToSearch As String, CaseSensitive As Boolean) As String
'Alias: Crazy
'CReplaceString Function
'This is a advanced replacestring.
'There are obviously the basic replace
'string options with the now included
'case sensitive.
'Casing being Ex: aKa, AkA, aka

'Dim our variables
Dim FoundLeft      As String
Dim FoundRight     As String
Dim Found        As Integer

'If in the string, get it's first location
If CaseSensitive = True Then
    Found = InStr(1, ToSearch$, ToFind$)
ElseIf CaseSensitive = False Then
    Found = InStr(1, LCase(ToSearch$), LCase(ToFind$))
End If

'If the string you want to replace
'is not there, Found will = 0, and this
'If/Then statement will be skipped.
'If Found is not equal to 0 (<>) then
'it will enter the If/Then statement
'and follow the rest of the function
If Found <> 0& Then
  
  Do
    
    
    FoundLeft = Left(ToSearch$, Found - 1)
    FoundRight = Mid(ToSearch$, Found + Len(ToFind$), Len(ToSearch$) - Found + Len(ToFind$))
    ToSearch$ = FoundLeft & ReplaceWith$ & FoundRight
    
    
    'Gets next location of string
    'that you want to replace
    If CaseSensitive = True Then
      Found = InStr(Found + 1, ToSearch$, ToFind$)
    ElseIf CaseSensitive = False Then
      Found = InStr(Found + 1, LCase(ToSearch$), LCase(ToFind$))
    End If
  Loop Until Found = 0& 'Will exit loop if no longer found
  
  'Set the new string
  CReplaceString = ToSearch$
ElseIf Found = 0& Then 'If what you are looking for
            'is not in the string then
            'it will just keep it the
            'same as it was when the
            'function was initiated
  CReplaceString = ToSearch$
End If
End Function
ความคิดเห็นดั้งเดิม (3)
กู้คืนจาก Wayback Machine