Need help how to proper split a string by crlf below is the code:
Dim str As String = "Hello" & vbCrLf & "World"
Dim parts As String() = str.Split(ControlChars.CrLf.ToCharArray)
For Each part As String In parts
MsgBox(part)
Next
Output
Hello
World
I want to get rid the blank space in between the two.
Hello
World
Vbcrlf is a combination of the two actions explained above: the Carriage Return (VbCr) that moves the cursor back to the beginning of the line and the Line feed (VbLf) that moves the cursor position down one line. In other words, vbCrLf is to VBA what the ENTER key is to a word processor.
One can only split on a char but in most cases NewLine is two characters, Carriage Return (0x0D AKA Char 13) and Line Feed (0x0A AKA Char 10). But in other systems it's just a LF. So I simply remove all instances of the CR and split on the LF.
Split We call Split() with a Char array with 1 element—the comma char. This separates each line on the comma. RemoveEmptyEntries. Sometimes there are no characters between two delimiters.
Use
str.Split(ControlChars.CrLf.ToCharArray(), StringSplitOptions.RemoveEmptyEntries)
instead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With