How to create a list containing a range of characters in VB.NET

2 Answers

0 votes
Imports System
Imports System.Collections.Generic
 
Public Class Program
    Public Shared Function CreateCharacterRangeList(ByVal start As Char, ByVal _end As Char) As List(Of Char)
        Dim charList As List(Of Char) = New List(Of Char)()
 
        Dim startCode As Integer = Convert.ToInt32(start)
        Dim endCode As Integer = Convert.ToInt32(_end)
 
        For chCode As Integer = startCode To endCode
            charList.Add(Convert.ToChar(chCode))
        Next
 
        Return charList
    End Function
 
    Public Shared Sub PrintCharacters(ByVal charList As List(Of Char))
        For Each ch As Char In charList
            Console.Write(ch & " ")
        Next
 
        Console.WriteLine()
    End Sub
 
    Public Shared Sub Main()
        Dim start As Char = "a"c
        Dim _end As Char = "m"c
 
        Dim charList As List(Of Char) = CreateCharacterRangeList(start, _end)
 
        PrintCharacters(charList)
    End Sub
End Class
 
 
 
' run:
'
' a b c d e f g h i j k l m 
'

 



answered Mar 21 by avibootz
edited Mar 21 by avibootz
0 votes
Imports System
Imports System.Linq
Imports System.Collections.Generic
 
Public Class Program
    Public Shared Function CreateCharacterRangeList(ByVal startChar As Char, ByVal endChar As Char) As List(Of Char)
        If endChar < startChar Then
            Throw New ArgumentException("End character must be greater than or equal to start character.")
        End If
         
        Return Enumerable.Range(Convert.ToInt32(startChar), Convert.ToInt32(endChar) - Convert.ToInt32(startChar) + 1).
                         Select(Function(i) Convert.ToChar(i)).
                         ToList()
    End Function
 
    Public Shared Sub PrintCharacters(ByVal charList As List(Of Char))
        For Each ch As Char In charList
            Console.Write(ch & " ")
        Next
 
        Console.WriteLine()
    End Sub
 
    Public Shared Sub Main()
        Dim start As Char = "a"c
        Dim _end As Char = "m"c
 
        Dim charList As List(Of Char) = CreateCharacterRangeList(start, _end)
 
        PrintCharacters(charList)
    End Sub
End Class
 
 
 
' run:
'
' a b c d e f g h i j k l m 
'

 



answered Mar 21 by avibootz
edited Mar 21 by avibootz
...