Results 1 to 5 of 5

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

  1. #1
    jsurpless is offline VB.NET Forum Fanatic
    .NET Framework
    .NET 2.0 (VS 2005)
    Join Date
    Jul 2008
    Posts
    139
    Reputation
    63

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

    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

    Code:
    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

  2. #2
    jsurpless is offline VB.NET Forum Fanatic
    .NET Framework
    .NET 2.0 (VS 2005)
    Join Date
    Jul 2008
    Posts
    139
    Reputation
    63
    Turns out it's actually a carriage-return or something similar...

  3. #3
    jmcilhinney's Avatar
    jmcilhinney is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    11,363
    Reputation
    1544
    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.

  4. #4
    jsurpless is offline VB.NET Forum Fanatic
    .NET Framework
    .NET 2.0 (VS 2005)
    Join Date
    Jul 2008
    Posts
    139
    Reputation
    63
    OK, I'm trying this

    strIA_Block_Parameters = IA_Block_Parameters_FileStreamReader.ReadToEnd.Spl it(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'

  5. #5
    jmcilhinney's Avatar
    jmcilhinney is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    11,363
    Reputation
    1544
    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.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Harvest time tracking