VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



String ReplaceTHE FASTEST

by Leigh Bowers (6 Submissions)
Category: String Manipulation
Compatability: Visual Basic 3.0
Difficulty: Unknown Difficulty
Date Added: Wed 3rd February 2021
Rating: (8 Votes)

This code is a direct replacement for the VB6 Replace function. Obviously, VB 6 users will have very little (if any) use for the function. It is intended for use with older versions of VB that do not have the in-built Replace function. I noticed a similar piece of code released earlier this week, but it only appeared to handle character replacing and not strings. This version will do both and it's pretty tight and fast.

Rate String ReplaceTHE FASTEST

Public Function Replace(sExpression As String, sFind As String, sReplace As String) As String
' Title: Replace
' Version: 1.01
' Author: Leigh Bowers
' WWW:  http://www.esheep.freeserve.co.uk/compulsion
Dim lPos As Long
Dim iFindLength As Integer
' Ensure we have all required parameters
 If Len(sExpression) = 0 Or Len(sFind) = 0 Then
  Exit Function
 End If
 
' Determine the length of the sFind variable
 iFindLength = Len(sFind)
 
' Find the first instance of sFind
 
 lPos = InStr(sExpression, sFind)
 
' Process and find all subsequent instances
 
 Do Until lPos = 0
  sExpression = Left$(sExpression, lPos - 1) + sReplace + Mid$(sExpression, lPos + iFindLength)
  lPos = InStr(lPos, sExpression, sFind)
 Loop
 
' Return the result
 Replace = sExpression
End Function

Download this snippet    Add to My Saved Code

String ReplaceTHE FASTEST Comments

No comments have been posted about String ReplaceTHE FASTEST. Why not be the first to post a comment about String ReplaceTHE FASTEST.

Post your comment

Subject:
Message:
0/1000 characters