README.md

December 4, 2022 ยท View on GitHub

Easily Replace a character in a string

Description

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

More Info

Submitted On
BySteve Berardi
LevelIntermediate
User Rating4.0 (8 globes from 2 users)
CompatibilityVB 4.0 (16-bit), VB 4.0 (32-bit), VB 5.0, VB 6.0
CategoryString Manipulation
WorldVisual Basic
Archive File

Source 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