-
Dec 13th, 2006, 08:36 PM
#1
Thread Starter
Frenzied Member
[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
-
Dec 13th, 2006, 08:43 PM
#2
Lively Member
Re: [02/03] What is the 'NewLine' char (to use with string.split)?
Visual Studio 2005 - Visual Basic and C#
Love that SQL
-
Dec 13th, 2006, 09:11 PM
#3
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:
Dim lines As String() = System.Text.RegularExpressions.Regex.Split(multiLineString, Environment.NewLine)
-
Dec 13th, 2006, 09:29 PM
#4
Thread Starter
Frenzied Member
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.
-
Dec 13th, 2006, 09:35 PM
#5
Re: [02/03] What is the 'NewLine' char (to use with string.split)?
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.
-
Dec 13th, 2006, 09:39 PM
#6
Lively Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|