PapaBless PapaBless - 14 days ago 5
Vb.net Question

Remove matching string from StringBuilder

How can I remove a certain

string
on a
StringBuilder
? Ex.
.append("Stack over flow dot com")
Remove
flow
and
.toString()
is now equal to
Stack over dot com
. I want to try
.remove()
but it only accepts
(startIndex, length)
. Do I have to search for the string first in the
StringBuilder
and store index and length to be used for
.remove(i,x)
? If possible, is there a better way to do it?

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"

Answer

Try below

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"

By the way, if you really want to find and then remove, refer the link

Comments