2016-11-22 69 views
1

如何刪除StringBuilder上的某個string?防爆。 .append("Stack over flow dot com")刪除flow.toString()現在等於Stack over dot com。我想嘗試.remove(),但它只接受(startIndex, length)。我是否必須首先在StringBuilder中搜索字符串,並存儲索引和長度以用於.remove(i,x)?如果可能,是否有更好的方法來做到這一點?從StringBuilder中刪除匹配的字符串

Dim someStrBlr As New StringBuilder 
Dim someStrRmv = "flow" 
Dim someString = "Stack over flow dot com" 
someStrBlr.append(someString) 
someStrBlr.remove(?) 'remove? "flow" 
someStrBlr.remove(?) 'replace? "flow" with "" 
Console.WriteLine(someStrBlr.toString()) "Stack over dot com" 

回答

3

試試下面

Dim someStrBlr As New StringBuilder 
Dim someStrRmv = "flow" 
Dim someString = "Stack over flow dot com" 
someStrBlr.Append(someString) 
someStrBlr.Replace(someStrRmv, "") 'removes the flow from string 
Console.WriteLine(someStrBlr.ToString()) '"Stack over dot com" 

順便說一句,如果你真的想找到並刪除,請參考link

+0

我就知道! ,我以爲'.Replace()'只接受'char'。我正要回答我自己的問題,但謝謝。 – PapaBless