How to remove trailing nulls (0) from byte List in VB.NET

1 Answer

0 votes
Imports System
Imports System.Collections.Generic

Public Class Program
    Public Shared Function RemoveTrailingNulls(ByVal byteList As List(Of Byte)) As List(Of Byte)
        While byteList.Count > 0 AndAlso byteList(byteList.Count - 1) = 0
            byteList.RemoveAt(byteList.Count - 1)
        End While

        Return byteList
    End Function

    Public Shared Sub Main()
        Dim byteList As List(Of Byte) = New List(Of Byte) From {
            1,
            2,
            3,
            0,
            0,
            0,
            0
        }
        Dim trimmedList As List(Of Byte) = RemoveTrailingNulls(byteList)

        For Each b As Byte In trimmedList
            Console.Write(b & " ")
        Next
    End Sub
End Class


' run:
'
' 1 2 3 
'

 



answered Mar 13, 2025 by avibootz

Related questions

2 answers 96 views
1 answer 97 views
1 answer 146 views
1 answer 105 views
2 answers 107 views
1 answer 93 views
1 answer 87 views
...