2011-03-22 233 views
15

我有这个字符串:VB.NET - 从字符串中删除字符

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

很好,谢谢!

+0

Visual Basic和VB.NET是不一样的东西(Visual Basic是不.NET语言)。 – Oded 2011-03-22 22:02:57

+0

这两者之间的区别是什么? – 2011-03-22 22:11:13

+2

考虑下一个开发人员,他们必须认识到你基本上已经包装了本地'string.replace'。换句话说,在不需要的时候添加更多的抽象。除此之外:您应该将答案标记为绿色复选标记的“已接受”。 – 2011-03-22 22:14:13

回答

7
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

16

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

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

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

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

可以使用替换方法

.replace( “字符被删除”, “性格与改为”)

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