VbNet · Console
Imports System
imports System.Linq
imports System.Collections.Generic
Public Module Module1
Public Sub Main()
Dim test As String = "A,A1,A2,A3,B,B1,B2,B3,C,C1,C2,C3"
'this gets A,B,C
Dim SingleChar As String() = test.Split(",").Where(Function(a) Not a.Any(Function(c) Char.IsDigit(c))).ToArray()
'this gets rest of the string
Dim CharWithNumbers As String() = test.Split(",").Where(Function(a) a.Any(Function(c) Char.IsDigit(c))).ToArray()
Dim StrFinal As String = ""
Dim diction = New Dictionary(Of String, List(Of String))()
Dim list = New List(Of String)()
'This will add similar strings found in "CharWithNumbers" based on
'the characters in "SingleChar" to a list
For Each c As char In SingleChar
list = New List(Of String)()
diction.Add(c, list)
For Each item As string In CharWithNumbers
If item.Contains(c) Then
list.Add(item)
End If
Next
Next
'grouping the list
For Each item In diction
StrFinal += item.Key + "(" + String.Join(",", item.Value) + "),"
Next
StrFinal = StrFinal.TrimEnd(","C)
'removes trailing ','
'to replace "," with "and"
Dim LastChar = SingleChar(SingleChar.Count() - 1)
StrFinal = StrFinal.Replace("," + LastChar + "(", " and " + LastChar + "(")
Console.WriteLine(StrFinal)
End Sub
End Module