检索以逗号分隔的字符串联接



预期输出: 我想返回 2 的组合,在下面的例子中,每个组合用逗号和vbcrlf分隔,例如:

1,2
3,4
5,6

等等...

问题是下面的代码没有。

它在行中返回我的值(1,2,3,4,5,6,7,8,9,10,11,12..等等。

如何使 button1 中的代码正常工作?

Function GetCombinations(ByVal depth As Integer, ByVal values As String()) As IEnumerable(Of String)
If depth > values.Count + 1 Then Return New List(Of String)
Dim result = New List(Of String)
For i = 0 To depth - 1
For y = 0 To values.Count - 1
If i = 0 Then
result.Add(values(y))
Else
result.Add(values(i - 1) + values(y))
End If
Next
Next
Return result
End Function
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim data_array As String() = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14",
"15"}
Dim result = GetCombinations(2, data_array)
Dim resultx As String = String.Join(",", result)
TxtListScanTxt.AppendText(resultx)
End Sub

这是一种方法-经过测试和工作

Public Sub Main()

Dim data_array As String() = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14",
"15"}
dim second as boolean
dim sb = new stringbuilder()
For i as integer = 0 To data_array.Length - 1
if second then
sb.AppendLine(data_array(i -1) & "," & data_array(i))
second = false
continue for
end if
if i = data_array.Length - 1 then
sb.AppendLine(data_array(i))
end if
second = true
next

Console.WriteLine(sb.ToString())
End Sub

我的解决方案

Dim data_array As String() = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14",
"15"}
Dim depth As Integer = 3, i As Integer = 1
Dim result As StringBuilder = New StringBuilder
For y = 0 To data_array.Count - 1
If i < depth Then
result.Append(data_array(y) + ",")
i += 1
Else
result.Append(data_array(y) + vbNewLine)
i = 1
End If
Next
RichTextBox1.AppendText(result.ToString)
Sub Main(args As String())
Dim arr = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15"}
'// First, we join all array values with comma to get a string.
'// Second, we find each group of two digits separated by comma (d+,d+)
'// and replace it ($1) with the same group PLUS the new-line character.
Dim x = Regex.Replace(String.Join(",", arr), "(d+,d+),?", $"$1{vbCrLf}")
End Sub

最新更新