[02/03] What is the 'NewLine' char (to use with string.split)?-VBForums
Results 1 to 6 of 6

Thread: [02/03] What is the 'NewLine' char (to use with string.split)?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Mar 2005
    Location
    Sector 001
    Posts
    1,579

    [02/03] What is the 'NewLine' char (to use with string.split)?

    The title is funny but I couldn't think of a more descriptive one. I am trying to split clipboard contents and want to do it with the string.split method and not with the Split function. Can't figure out the new line delimiter, so
    Code:
    Text
    Text
    Text
    Text
    ends up with empty lines in between:
    PHP Code:
    Text

    Text

    Text

    Text 
    VB 2005, Win Xp Pro sp2

  2. #2
    Lively Member
    Join Date
    Oct 2006
    Posts
    86

    Re: [02/03] What is the 'NewLine' char (to use with string.split)?

    vbcrlf
    Visual Studio 2005 - Visual Basic and C#
    Love that SQL

  3. #3
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    87,325

    Re: [02/03] What is the 'NewLine' char (to use with string.split)?

    Don't use vbCrLf in VB.NET at all. You should use ControlChars.NewLine or Environment.NewLine.

    The problem is that the .NET 1.x version of String.Split doesn't take strings as delimiters. It will only split on single characters as delimiters. If you try to split on line breaks, which is a string that is a combination of a carriage return character and a line feed character, then one of two things can happen, depending on the situation.

    1. The CrLf String will be implicitly converted to a Char by simply taking the first character. This means that you'll be splitting on the carriage return only, thus leaving the line feed behind in the results. When you try to display the results those line feeds will be displayed as line breaks.

    2. The CrLf String will be split into two characters and the original string will be split on every carriage return and every line feed. This will add empty strings to your results for the substrings that are between each carriage return and line feed pair.

    The .NET 2.0 String.Split method adds some additional overloads that do take strings as delimiters. In .NET 1.x I suggest that you use Regex.Split instead, e.g.
    VB Code:
    1. Dim lines As String() = System.Text.RegularExpressions.Regex.Split(multiLineString, Environment.NewLine)

    2007-2014

    Why is my data not saved to my database? | MSDN Data Walkthroughs
    MSDN "How Do I?" Videos: VB | C#
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts) | WP8 Turnstile Feather Transition with Pivot Control
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Mar 2005
    Location
    Sector 001
    Posts
    1,579

    Re: [02/03] What is the 'NewLine' char (to use with string.split)?

    Well it appears that even controlchars.vbCrLf is not a char but actually a string. I could use .Replace(vbCrLf, vbCr).Split(Controlchars.Cr) but I guess I tortured the framework enough for today.

    Thanks jmcilhinney, will use your idea.
    VB 2005, Win Xp Pro sp2

  5. #5
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    87,325

    Re: [02/03] What is the 'NewLine' char (to use with string.split)?

    Quote Originally Posted by Half
    Well it appears that even controlchars.vbCrLf is not a char but actually a string.
    I already said that. ControlChars.CrLf and ContolChars.NewLine are both a carriage return (Cr) and line feed (Lf) pair. Two characters can never be one character. The difference between ControlChars.NewLine and Environment.NewLine is that one is a constant and the other isn't. Environment.NewLine returns the line break sequence for the current environment. That means that under Mono on Linux/Unix it will return just a line feed, while on Windows it will return a carriage return and line feed. ControlChars.NewLine is defined as a carriage return and line feed so it will not change regardless of the platform.

    2007-2014

    Why is my data not saved to my database? | MSDN Data Walkthroughs
    MSDN "How Do I?" Videos: VB | C#
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts) | WP8 Turnstile Feather Transition with Pivot Control
    Beginner Tutorials: VB | C# | SQL

  6. #6
    Lively Member
    Join Date
    Oct 2006
    Posts
    86

    Re: [02/03] What is the 'NewLine' char (to use with string.split)?

    Hmm - I have used it and never had a problem, but this gives me something to think about.
    Visual Studio 2005 - Visual Basic and C#
    Love that SQL

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.