StreamReader On CSV => List (Of String) - Leading Spaces

jsurpless

Well-known member
Joined
Jul 29, 2008
Messages
144
Programming Experience
Beginner
I've got a CSV that's just a single column of text like so

Param1
Param2
Param3

and so on...

I'm trying to use

VB.NET:
Dim strIA_Block_Parameters As New List(Of String)

                Dim IA_Block_Parameters_FileStream As FileStream = New FileStream(strIA_Block_Parameters_FilePath, FileMode.Open, FileAccess.Read)
                Dim IA_Block_Parameters_FileStreamReader As StreamReader = New StreamReader(IA_Block_Parameters_FileStream)

                '-----

                strIA_Block_Parameters = IA_Block_Parameters_FileStreamReader.ReadToEnd.Split(ControlChars.NewLine).ToList

to read it into a List of String... the issue is that when I use this, every entry after the 1st gets a leading space...

Any thoughts on this?

Thanks!

-Justin
 
The problem is the way you're calling Split. You should have Option Strict On and it would pull you up on that. You are calling an overload of Split that accepts a single Char to split on but you are passing a String containing two Chars: a carriage return and a line feed. With Option Strict On that would fail to compile, drawing your attention to the error before run time. With Option Strict Off, that String is implicitly converted to a Char simply by dropping all but the first character. As such, you are splitting on the carriage returns and leaving the line feeds in the substrings. You need to use an overload of Split that will actually split on multi-character strings so that you can split on the carriage return/line feed pairs.
 
OK, I'm trying this

strIA_Block_Parameters = IA_Block_Parameters_FileStreamReader.ReadToEnd.Split(ControlChars.CrLf, StringSplitOptions.RemoveEmptyEntries)

but ControlChars.CrLf doesn't compile, yielding an error of 'Argument matching parameter separator narrows to string to 1-dim array of Char'
 
You can't just make up methods. ControlChars.CrLf is a String. Is there any overload of String.Split that has two parameters of types String and StringSplitOptions? No, there isn't you can only pass arguments of types accepted by the method you're trying to call.
 
Back
Top