Gowtama Krishna Gowtama Krishna - 5 months ago 51
Vb.net Question

String builder count coma and find position vb.net

HI I am trying to map two string builder.

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

both string builders 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 coma or could anyone suggest me best practice to remove the string I don't want in the appropriate position.


So I see 4 cases:

  • equals to "abc2" (seems very unlikely)
  • contains ",abc2,"
  • starts with "abc2,"
  • ends with ",abc2"

Sub removeCSVvalue(sb As StringBuilder, value$)
    If sb.Length <= value.Length Then sb.Replace(value, vbNullString) : Return ' If sb = value
    sb.Replace("," & value & ",", ",")   ' Example: replace ",abc2," with ","
    Dim C = value.Length
    If sb.Length > C Then If sb(C) = "," Then If sb.ToString(0, C) = value Then sb.Remove(0, C + 1) ' If sb.StartsWith(value & ",")
    Dim L = sb.Length - C
    If sb.Length > C Then If sb(L - 1) = "," Then If sb.ToString(L, C) = value Then sb.Length = L - 1 ' If sb.EndsWith("," & value)
End Sub

P.S. this reminds me of my first downvoted answer Remove item from string array :]