VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



Easily Replace a character in a string

by Steve Berardi (3 Submissions)
Category: String Manipulation
Compatability: Visual Basic 3.0
Difficulty: Intermediate
Date Added: Wed 3rd February 2021
Rating: (2 Votes)

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

Rate Easily Replace a character in a string

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

Download this snippet    Add to My Saved Code

Easily Replace a character in a string Comments

No comments have been posted about Easily Replace a character in a string. Why not be the first to post a comment about Easily Replace a character in a string.

Post your comment

Subject:
Message:
0/1000 characters