VB.NET - 从字符串中删除字符

23

我有这个字符串:

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, "")

太好了,谢谢!


Visual Basic和VB.NET不是同一件事(Visual Basic是非.NET语言)。 - Oded
这两者之间到底有什么区别? - Jonathan Rioux
2
考虑下一个开发者,他将不得不意识到你基本上包装了本地的 string.replace。换句话说,在不必要的情况下添加更多的抽象。另外:你应该用绿色的勾号标记答案为“已接受”。 - p.campbell
1
Visual Basic是随Visual Studio 6一起发布的,而VB.NET则是一种现代的.NET语言。 - Oded
4个回答

22

String类中有一个Replace方法,可以完成这个任务。

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

16
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的Replace函数的更多信息。


5

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

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

0
你可以使用 string.replace 方法 string.replace("要删除的字符", "要替换为的字符")
Dim strName As String
strName.Replace("[", "")

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接