我有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是顶部选定的字符串组,则它将是:
输出将是(由第3个字符串组):
"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),那将会更好。
答案 0 :(得分:0)
这是我第一次尝试你的回答。不幸的是,它还没有动态。它使用LINQ及其交叉连接功能来获得答案。
Private Sub btnMiscTest_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnMiscTest.Click
Dim str1 As String = "A1,A2"
Dim str2 As String = "2,3"
Dim str3 As String = "4A,4B"
Dim str4 As String = "7,8"
'Add the strings into a list
Dim multiString As New List(Of String)
multiString.Add(str1)
multiString.Add(str2)
multiString.Add(str3)
multiString.Add(str4)
Dim resultStrings As List(Of String) = GenerateString(multiString, 3)
End Sub
Private Function GenerateString(ByVal multiString As List(Of String), ByVal sortIndex As Integer) As List(Of String)
'Split each string into a list of strings and add it to a "StringOfStrings" list
Dim tmpStringOfStrings As New List(Of List(Of String))
For Each tmpString As String In multiString
Dim tmpSubString As List(Of String) = tmpString.Split(",").ToList
tmpStringOfStrings.Add(tmpSubString)
Next
'Use LINQ to do a Cross Apply and sort by index
Dim resultList As List(Of String) = (From s0 As String In tmpStringOfStrings(0) _
From s1 As String In tmpStringOfStrings(1) _
From s2 As String In tmpStringOfStrings(2) _
From s3 As String In tmpStringOfStrings(3) _
Order By If(sortIndex = 0, s0, _
If(sortIndex = 1, s1, _
If(sortIndex = 2, s2, s3))) _
Select s0 & "-" & s1 & "-" & s2 & "-" & s3).ToList
Return resultList
End Function