我正在尝试映射两个StringBuilders:
string builder1 ="abc1,abc2,abc3,abc4,abc2"
string builder2="100,30,15,102,30"
两个StringBuilders计数都是相同的并且已经映射。但我正在搜索特定号码30
并命名为abc2
即find(30,abc2)
并将其删除
所以我需要跟踪逗号,或者有人建议我最好的做法是删除我不想要的字符串。
答案 0 :(得分:1)
不使用正则表达式,可能更容易将字符串转换为列表,删除项目,然后重新创建列表:
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());
对于VB.Net版本:
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())
答案 1 :(得分:1)
所以我看到4个案例:
"abc2"
(似乎非常不可能)",abc2,"
"abc2,"
",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。这让我想起了我的第一个低估答案Remove item from string array:]