如何删除string
上的某个StringBuilder
?防爆。 .append("Stack over flow dot com")
删除flow
,.toString()
现在等于Stack over dot com
。我想尝试.remove()
,但它只接受(startIndex, length)
。我是否必须先在StringBuilder
中搜索字符串并存储用于.remove(i,x)
的索引和长度?如果可能的话,还有更好的方法吗?
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"
答案 0 :(得分: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
答案 1 :(得分:1)
Dim someStrBlr As New StringBuilder
Dim someStrRmv = "flow"
Dim someString = "Stack over flow dot com"
someStrBlr.append(someString)
someStrBlr.replace(someStrRmv,"") ' Replace with empty string
Console.WriteLine(someStrBlr.toString())
参考:https://msdn.microsoft.com/en-us/library/3h5afh4w(v=vs.110).aspx