VB.NET - Remove a characters from a String

159,201

Solution 1

Function RemoveCharacter(ByVal stringToCleanUp, ByVal characterToRemove)
  ' replace the target with nothing
  ' Replace() returns a new String and does not modify the current one
  Return stringToCleanUp.Replace(characterToRemove, "")
End Function

Here's more information about VB's Replace function

Solution 2

The String class has a Replace method that will do that.

Dim clean as String
clean = myString.Replace(",", "")

Solution 3

The string class's Replace method can also be used to remove multiple characters from a string:

Dim newstring As String
newstring = oldstring.Replace(",", "").Replace(";", "")
Share:
159,201
Jonathan Rioux
Author by

Jonathan Rioux

DevOps Engineer at Intact Financial Corporation

Updated on February 11, 2020

Comments

  • Jonathan Rioux
    Jonathan Rioux over 4 years

    I have this string:

    Dim stringToCleanUp As String = "bon;jour"
    Dim characterToRemove As String = ";"
    

    I want a function who removes the ';' character like this:

    Function RemoveCharacter(ByVal stringToCleanUp, ByVal characterToRemove)
    ...
    End Function
    

    What would be the function ?

    ANSWER:

    Dim cleanString As String = Replace(stringToCleanUp, characterToRemove, "")
    

    Great, Thanks!