How to convert a character (flatten) matrix to a single string in VB.NET

1 Answer

0 votes
Imports System
Imports System.Text
Imports System.Collections.Generic

Module Module1

    Function FlattenMatrix(matrix As List(Of List(Of Char))) As String
        Dim sb As New StringBuilder()

        For Each row In matrix
            For Each ch In row
                sb.Append(ch)
            Next
        Next

        Return sb.ToString()
    End Function

    Sub Main()
        Dim matrix As New List(Of List(Of Char)) From {
            New List(Of Char) From {"H"c, "e"c, "l"c, "l"c, "o"c},
            New List(Of Char) From {" "c, "W"c, "o"c, "r"c, "l"c, "d"c},
            New List(Of Char) From {"!"c}
        }

        Dim output As String = FlattenMatrix(matrix)

        Console.WriteLine("Flattened string: " & output)
    End Sub

End Module


' run:
'
' Flattened string: Hello World!
'

 



answered Jan 10 by avibootz

Related questions

...