我依次有 4 个文本框供用户输入字符串。如果字符串包含逗号,它将被拆分。对于每个字符串,它将用 Dash 与下一个字符串连接。可视化这个问题:
Dim str1 = "A1,A2"
Dim str2 = "2,3"
Dim str3 = "4A,4B"
Dim str4 = "7,8"
格式:str1 & "-" & str2 & "-" & str3 & "-" & str4
输出将是(按升序):
{"A1-2-4A-7", "A1-2-4A-8", "A1-2-4B-7", "A1-2-4B-8",
"A1-3-4A-7", "A1-3-4A-8", "A1-3-4B-7", "A1-3-4B-8",
"A2-2-4A-7", "A2-2-4A-8", "A2-2-4B-7", "A2-2-4B-8",
"A2-3-4A-7", "A2-3-4A-8", "A2-3-4B-7", "A2-3-4B-8"}
如果没有逗号,它将被视为单个字符串。
我设法通过以下方式实现了上述结果:
Private Sub GenerateString(ByVal str1 As String, ByVal str2 As String, ByVal str3 As String, ByVal str4 As String)
Dim arr1 As New List(Of String)
Dim arr2 As New List(Of String)
Dim arr3 As New List(Of String)
Dim arr4 As New List(Of String)
SpliString(str1, arr1)
SpliString(str2, arr2)
SpliString(str3, arr3)
SpliString(str4, arr4)
Dim arrMain As New ArrayList
arrMain.Add(arr1)
arrMain.Add(arr2)
arrMain.Add(arr3)
arrMain.Add(arr4)
Dim listCom As New List(Of String)
For Each tempList As List(Of String) In arrMain
If tempList.Count > 0 Then
If listCom.Count = 0 Then
listCom.AddRange(tempList)
Else
Dim listTemp As New List(Of String)
listTemp.AddRange(listCom)
listCom.Clear()
For Each tempStrMain As String In listTemp
For Each tempStr As String In tempList
listCom.Add(tempStrMain & "-" & tempStr)
Next
Next
End If
End If
Next
Return listCom
End Function
Private Sub SpliString(ByVal strToSplit As String, ByRef arrString As List(Of String))
If Not strToSplit = "" Then
If strToSplit.Contains(",") Then
Dim splitArr() As String = strToSplit.Split(",")
For Each str As String In splitArr
arrString.Add(str)
Next
Else
arrString.Add(strToSplit)
End If
End If
End Sub
现在我的问题是,如何按字符串组对字符串进行排序?例如,如果 str3 是顶部选定的字符串组,它将是:
输出将是(由第三个字符串组):
"A1-2-4A-7", "A1-2-4A-8", "A1-3-4A-7", "A1-3-4A-8",
"A2-2-4A-7", "A2-2-4A-8", "A2-3-4A-7", "A2-3-4A-8",
"A1-2-4B-7", "A1-2-4B-8", "A1-3-4B-7", "A1-3-4B-8",
"A2-2-4B-7", "A2-2-4B-8", "A2-3-4B-7", "A2-3-4B-8"
如果 str4 是要排序的选定组,则输出:
"A1-2-4A-7", "A1-2-4B-7", "A1-3-4A-7", "A1-3-4B-7",
"A2-2-4A-7", "A2-2-4B-7", "A2-3-4A-7", "A2-3-4B-7",
"A1-2-4A-8", "A1-2-4B-8", "A1-3-4A-8", "A1-3-4B-8",
"A2-2-4A-8", "A2-2-4B-8", "A2-3-4A-8", "A2-3-4B-8"
使用 DataTable、ArrayList、IComparer 之类的任何方法?而且,有没有更好的方法来重写 GenerateString 代码?如果代码足够动态以支持 n 个输入(目前我将其硬编码为 4),那就更好了。