Tools Links Login

A 'strReplace' function.

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".

Original Author: Danny Young

Inputs

OldString, OldLetter and NewLetter

Assumptions

AND THIS IS FOR VB5.....

Returns

The modified string

Side Effects

None.

Code

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

About this post

Posted: 2002-06-01
By: ArchiveBot
Viewed: 124 times

Categories

Visual Basic 6

Attachments

No attachments for this post


Loading Comments ...

Comments

No comments have been added for this post.

You must be logged in to make a comment.