Advertisement
C_Volume2 String Manipulation #76113

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.

AI

AI Summary: This codebase represents a historical implementation of the logic described in the metadata. Our preservation engine analyzes the structure to provide context for modern developers.

Source Code
original-source
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

Upload
Original Comments (3)
Recovered from Wayback Machine