我正在尝试将字符串列表的元素组合在一起,即a,b,c,d的列表中;我想看看ab,bc,cd。这是我到目前为止的代码:
Dim C As New List(Of String)
For i = 0 To S.Count - 1
For j =i + 1 To S.Count - 1
C.Add("{" & S(i) & "," & S(j) & "}")
Next
Next
Dim value As String = String.Join(",", C)
TextBox2.Text = value
目前,此代码返回的功能集为{a,b},{a,c},{a,d},{b,c},{b,d},{c,d} .. < / p>
有一种有效的方法吗?
答案 0 :(得分:1)
看起来你只想要相邻的对,所以这对你有用:
Dim C As New List(Of String)
For i = 0 To S.Count - 2
C.Add("{" & S(i) & "," & S(i + 1) & "}")
Next
Dim value As String = String.Join(",", C)
TextBox2.Text = value
如果是这样,更简单:
Dim C As New List(Of String)
For i = 0 To S.Count - 2
C.Add(String.Format("{{{0},{1}}}", {S(i), S(i + 1)}))
Next
TextBox2.Text = String.Join(",", C)
答案 1 :(得分:0)
就这么简单:
Dim input As String = "a,b,c,d"
Dim output As String = String.Empty
Dim toggleIgnoreComma As Boolean = True
For Each s As String In input
If s.Equals(","c) Then
If toggleIgnoreComma Then
toggleIgnoreComma = False
Continue For
Else
toggleIgnoreComma = True
End If
End If
output += s
Next