VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



A 'strReplace' function.

by Danny Young (3 Submissions)
Category: String Manipulation
Compatability: Visual Basic 3.0
Difficulty: Intermediate
Date Added: Wed 3rd February 2021
Rating: (15 Votes)

When called with a string, will search through the string and replace a character of your choice with another character of your choice. For example, if you sent the string:
"Hello to the world"
And sent "o" as the character to be replaced,
and sent "a" as the replacement
It will return you with:
"Hella ta the warld".

Inputs
OldString, OldLetter and NewLetter
Assumes
AND THIS IS FOR VB5.....
Code Returns
The modified string
Side Effects
None.

Rate A 'strReplace' function.

Private Sub Command1_Click()
  Dim oldstring As String, newletter As String, oldletter As String, newstring As String
  oldstring = "hello To the world"
  newletter = "YEAH"
  oldletter = "hello"
  newstring = Replace(oldstring, newletter, oldletter)
  MsgBox newstring
End Sub

Public Function Replace(oldstring, newletter, oldletter) As String
  Dim i As Integer
  i = 1

  Do While InStr(i, oldstring, oldletter, vbTextCompare) <> 0
    Replace = Replace & Mid(oldstring, i, InStr(i, oldstring, oldletter, vbTextCompare) - i) & newletter
    i = InStr(i, oldstring, oldletter, vbTextCompare) + Len(oldletter)
  Loop
  Replace = Replace & Right(oldstring, Len(oldstring) - i + 1)
End Function

Download this snippet    Add to My Saved Code

A 'strReplace' function. Comments

No comments have been posted about A 'strReplace' function.. Why not be the first to post a comment about A 'strReplace' function..

Post your comment

Subject:
Message:
0/1000 characters