VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



Function to search for and replace text in a string variable. Not case sensitive!

by Joe Dacy II (6 Submissions)
Category: String Manipulation
Compatability: Visual Basic 4.0 (32-bit)
Difficulty: Unknown Difficulty
Originally Published: Sat 25th March 2000
Date Added: Mon 8th February 2021
Rating: (1 Votes)

Function to search for and replace text in a string variable. Not case sensitive!

Rate Function to search for and replace text in a string variable. Not case sensitive!




' Task1 is the searched for text
' Task2 is the replacement text
' Replacing is the complete text to be searched in a string variable
' MainText = ReplacingText(SearchFor, ReplaceWith, FullText)

Dim WD As Integer
Dim FirstPart As String
Dim LastPart As String
Dim MD As Integer
Dim Temporary As String
Dim NewReplace As String
Dim SF As String
Dim WE As Integer

' capitalize the strings to render the search not case sensitive

SF = UCase(Task1)
WE = InStr(UCase(Task2), SF)

' if the replacement text is contained in the searched for text, first replace the searched for text with a nonsense string.

If WE Then
NewReplace = "#$%@"
Another2:
Temporary = UCase(Replacing)
WD = InStr(Temporary, SF)
    If WD Then
    MD = WD + Len(Task1)
    FirstPart = Mid(Replacing, 1, WD - 1)
    LastPart = Mid(Replacing, MD, Len(Replacing))
    Replacing = FirstPart & NewReplace & LastPart
    GoTo Another2
    End If
SF = UCase("#$%@")
End If

' replace either the orginal searched for text with the replacement text, or replace the nonsense string with the replacement text

Another:
Temporary = UCase(Replacing)
WD = InStr(Temporary, SF)
If WD Then
MD = WD + Len(SF)
FirstPart = Mid(Replacing, 1, WD - 1)
LastPart = Mid(Replacing, MD, Len(Replacing))
Replacing = FirstPart & Task2 & LastPart
GoTo Another
End If

' place the result of the string for processing by the calling command
ReplacingText = Replacing

End Function



Download this snippet    Add to My Saved Code

Function to search for and replace text in a string variable. Not case sensitive! Comments

No comments have been posted about Function to search for and replace text in a string variable. Not case sensitive!. Why not be the first to post a comment about Function to search for and replace text in a string variable. Not case sensitive!.

Post your comment

Subject:
Message:
0/1000 characters