Question Split with Enter key?

Kayot

Well-known member
Joined
Mar 6, 2007
Messages
49
Programming Experience
3-5
Silly question, how do I split a string that has enter keys in it?

I tryed this:
VB.NET:
Dim Lines() As String = RawData.Split(Chr(Keys.Enter))
 
Usually linebreaks means vbNewline or equivalent, which is a string consisting of two chars, anyway one of the String.Split methods allows to use an array of strings as separators, using this with vbNewline like this:
VB.NET:
Dim s As String = "1" & vbNewLine & "2"
Dim items() As String = s.Split(New String() {vbNewLine}, StringSplitOptions.None)
items is not an array containing the two strings "1" and "2".
 
Or Environment.NewLine or ControlChars.NewLine

This will work too:
VB.NET:
Dim s As String = "1" & Environment.NewLine & "2"
Dim items() As String = s.Split(Environment.NewLine)
 
Actually not, that will leave a vbLF at the start of every item because it only splits at the first vbCR.
Below variant on the other hand will achieve the correct result at the expense of more processing. (it produces one empty item each linebreak, but remove empty entries)
VB.NET:
Dim items() As String = s.Split(Environment.NewLine.ToCharArray, StringSplitOptions.RemoveEmptyEntries)
There is also Regex that can split by String separators:
VB.NET:
Dim items() As String = System.Text.RegularExpressions.Regex.Split(s, vbNewLine)
 
Actually not, that will leave a vbLF at the start of every item because it only splits at the first vbCR.
Below variant on the other hand will achieve the correct result at the expense of more processing. (it produces one empty item each linebreak, but remove empty entries)
VB.NET:
Dim items() As String = s.Split(Environment.NewLine.ToCharArray, StringSplitOptions.RemoveEmptyEntries)
There is also Regex that can split by String separators:
VB.NET:
Dim items() As String = System.Text.RegularExpressions.Regex.Split(s, vbNewLine)

Did this change in VS 2005? In VS 2003 I've done it this way "Dim items() As String = s.Split(Environment.NewLine)" with no problems, the vbLF was removed as well.
 
No, it is the same in VB2003. Perhaps you were processing text that only had vbCr linebreaks?
 
The provided exaple works perfectly, however when it tried to array a set of strings from a textbox it dosen't divide the text at each return.

VB.NET:
Dim RawData As String = txtScript.Text
Dim Lines() As String = RawData.Split(New String() {vbNewLine}, StringSplitOptions.None)
For i As Byte = 0 To Lines.Length - 1
    Console.WriteLine(Lines(i) & " NEXT")
Next

When it runs it outputs everything from the textbox and then puts NEXT at the end. Heres the input:

VB.NET:
ac=2
stacksize=1
weight=2
price=0,2,0,0
augmentlevels=7,0,0,0,0
slots=head

Outputs

VB.NET:
ac=2
stacksize=1
weight=2
price=0,2,0,0
augmentlevels=7,0,0,0,0
slots=head
 NEXT

And what I wanted it to do:

Array(0) = "ac=2"
Array(1) = "stacksize=1"
Array(2) = "weight=2"
Array(3) = "price=0,2,0,0"
Array(4) = "augmentlevels=7,0,0,0,0"
Array(5) = "slots=head"

Keep in mind the textbox can have more or less lines and the lines will later be arrayed by a split using the = contained in each line of this array during a loop.

P.S. Nevermind, the
VB.NET:
.Split(Environment.NewLine.ToCharArray, StringSplitOptions.RemoveEmptyEntries)
Worked perfectly. I forgot to update the forum page, thank you very much for your assistance.
 
A multiline textbox already provide a string array of the lines, so you don't need to split Text, just use the Lines property.
 
Ah I see, I knew there was something like that, but I missed it on my first pass through the properties.

Is there a way to remove empty lines from the Textbox array when transferring it to another array?
 
Is there a way to remove empty lines
Yes, that is the easy part:
VB.NET:
For Each line As String In tb.Lines
    If line <> String.Empty Then
        ' you got a line that is not empty
    End If
Next
 
Then how do you remove blank lines that are already in an array? Do you loop it into a new array and remove the lines in that process or is there a vb.net single liner that does that for me?
 
Yes, you have to work it either way. Arrays are immutable, which means once an array is created you can't change it. Luckily lists/collections make this work you if you need it, which you don't in your Console.WriteLine code above.
 
Have you thought about putting up a wiki? Your like a walking VB.net programming wiki.

Thank you for the help, (I can't rep you till I rep some other people though T-T)
 
Thanks, you gotta spread the love ;)
 
Back
Top