VB.NET - Remove a characters from a String

2020-07-01 03:23发布

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!

4条回答
贼婆χ
2楼-- · 2020-07-01 03:28

You can use the string.replace method

string.replace("character to be removed", "character to be replaced with")

Dim strName As String
strName.Replace("[", "")
查看更多
▲ chillily
3楼-- · 2020-07-01 03:44

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(";", "")
查看更多
smile是对你的礼貌
4楼-- · 2020-07-01 03:47
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

查看更多
趁早两清
5楼-- · 2020-07-01 03:52

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

Dim clean as String
clean = myString.Replace(",", "")
查看更多
登录 后发表回答