VB.NET - 从String中删除字符

时间:2011-03-22 21:57:25

标签: vb.net string visual-studio-2010

我有这个字符串:

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

我想要一个删除';'的函数像这样的人物:

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

功能是什么?

解答:

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

太好了,谢谢!

4 个答案:

答案 0 :(得分:16)

String类有一个Replace方法可以做到这一点。

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

答案 1 :(得分:12)

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

以下是有关VB's Replace function

的更多信息

答案 2 :(得分:4)

string类的Replace方法也可用于从字符串中删除多个字符:

Dim newstring As String
newstring = oldstring.Replace(",", "").Replace(";", "")

答案 3 :(得分:0)

您可以使用字符串 .replace方法

string .replace(" 要删除的字符"," 字符将替换为 &#34)

Dim strName As String
strName.Replace("[", "")