1

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"
PapaBless
  • 53
  • 2
  • 7

2 Answers2

3

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

Community
  • 1
  • 1
Mukul Varshney
  • 3,131
  • 1
  • 12
  • 19
  • I knew it!, I thought `.Replace()` only accepts `char`. I was about to answer my own question, but thanks. – PapaBless Nov 22 '16 at 04:22
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()) 

Ref: https://msdn.microsoft.com/en-us/library/3h5afh4w(v=vs.110).aspx

Shep
  • 638
  • 3
  • 15