2011-03-22 230 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("[", "")