Home > front end >  convert start of string to index students by split text vb.net
convert start of string to index students by split text vb.net

Time:01-13

i have list of students degrees in listbox like this :

0,11,41,50
1,5,66,75
1,10,40,50
2,3,43,50
2,7,63,75
2,11,46,50

i need to make list like that

student 0 failed in subject num 11 & old degree is 41 and new degree is 50
student 1 failed in subject num 5 & old degree is 66 and new degree is 75 , failed in subject num 10 & old degree is 40 and new degree is 50
student 2 failed in subject num 3 & old degree is 43 and new degree is 50 , failed in subject num 7 & old degree is 63 and new degree is 75 , failed in subject num 11 & old degree is 46 and new degree is 50

CodePudding user response:

Try this:

    Dim ListOfStudents As New List(Of String)
    Dim Result As String = ""
    Dim MaxColumns As Integer = 1
    Dim CurrentColumn As Integer = 0

    For i = 0 To ListBox1.Items.Count - 1
        Dim Data As String() = ListBox1.Items(i).ToString().Split(",")

        ListOfStudents.Add("student " & Data(0) & " failed in subject num " & Data(1) & " & old degree is " & Data(2) & " and new degree is " & Data(3))
    Next
    For i = 0 To ListOfStudents.Count - 1
        If CurrentColumn = MaxColumns Then
            MaxColumns  = 1
            CurrentColumn = 0
            Result = Result.Remove(Result.Length - 3)
            Result &= vbCrLf
        End If
        Result &= ListOfStudents(i) & " , "
        CurrentColumn  = 1
    Next
    Result = Result.Remove(Result.Length - 3)    

I assume that your listbox is named ListBox1. You could display the result like this:

TextBox1.Text = Result

CodePudding user response:

Pretty similar to your last question...

Dim data() As String = {
    "0,11,41,50",
    "1,5,66,75",
    "1,10,40,50",
    "2,3,43,50",
    "2,7,63,75",
    "2,11,46,50"
}

Dim buckets As New Dictionary(Of String, List(Of String))
For Each inputSet In data
    Dim values As New List(Of String)(inputSet.Split(","))
    Dim studentNumber As String = values(0)
    values.RemoveAt(0)
    If Not buckets.ContainsKey(studentNumber) Then
        buckets.Add(studentNumber, New List(Of String))
    End If
    Dim msg As String = "failed in subject num " & values(0) &
                  " & old degree is " & values(1) &
                  " new degree is " & values(2)
    buckets(studentNumber).Add(msg)
Next

For Each kvp As KeyValuePair(Of String, List(Of String)) In buckets
    Dim msg As String = "student " & kvp.Key & " " & String.Join(", ", kvp.Value)
    Console.WriteLine(msg)
Next
  •  Tags:  
  • Related