Gowtama Krishna Gowtama Krishna - 1 year ago 90
Vb.net Question

StringBuilder count comma and find position

I am trying to map two StringBuilders:

string builder1 ="abc1,abc2,abc3,abc4,abc2"
string builder2="100,30,15,102,30"

both StringBuilders count will be the same and already mapped. But I am searching for a particular number
and name like

and remove it

so I need to keep track of the comma or could anyone suggest me best practice to remove the string I don't want in the appropriate position.

Answer Source

Without using Regex, it's probably easier to turn the string into a list, remove the items, then recreate the list:

StringBuilder sb1 = new StringBuilder("abc1,abc2,abc3,abc4,abc2");
string find1 = "abc2";
var newList = sb1.ToString().Split(',').ToList();
newList.RemoveAll(x => x == find1);
string result = string.Join(",", newList.ToArray());

For the VB.Net version:

Dim sb1 As New StringBuilder("abc1,abc2,abc3,abc4,abc2")
Dim find1 As String = "abc2"
Dim newList As New List(Of String)(sb1.ToString().Split(","c).ToList)
newList.RemoveAll(Function(x) x = find1)
Dim result As String = String.Join(",", newList.ToArray())
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download