Tools Links Login

Easily Replace a character in a string

Replaces a character in a string with another character. Pretty simple to understand. Uses a For...Next loop.

Original Author: Steve Berardi

Code

Function ReplaceCharacter(stringToChange$, charToReplace$, replaceWith$) As String
'Replaces a specified character in a string with another
'character that you specify
  Dim ln, n As Long
  Dim NextLetter As String
  Dim FinalString As String
  Dim txt, char, rep As String
  txt = stringToChange$ 'store all arguments in
  char = charToReplace$ 'new variables
  rep = replaceWith$
    
  ln = Len(txt)
  
  For n = 1 To ln Step 1
    NextLetter = Mid(txt, n, 1)
    
    If NextLetter = char Then
      NextLetter = rep
    End If
    
    FinalString = FinalString & NextLetter
  Next n
  
  Replace_Character = FinalString
  
End Function

About this post

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

Categories

Visual Basic 6

Attachments

No attachments for this post


Comments

No comments have been added for this post.

You must be logged in to make a comment.