for example .. If I have a text like this
214asd
df5df8
d66f66
I want to split them into 3 strings using vb.net .
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.
To split a string on newlines, you can use the regular expression '\r?\ n|\r' which splits on all three '\r\n' , '\r' , and '\n' . A better solution is to use the linebreak matcher \R which matches with any Unicode linebreak sequence. You can also split a string on the system-dependent line separator string.
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.
What is a SubString? The substring function is used to obtain a part of a specified string. This method is defined in the String class of Microsoft VB.NET. You have to specify the start index from which the String will be extracted. The String will be extracted from that index up to the length that you specify.
Assuming you want to split on new lines - using String.Split
will return an array containing the parts:
Dim parts As String() = myString.Split(new String() {Environment.NewLine},
StringSplitOptions.None)
This will be platform specific, so you may want to split on "\n"
, "\r"
, "\n\r"
or a combination of them. String.Split
has an overload that takes a string array with the strings you wish to split on.
Dim strLines() As String = Clipboard.GetText.Replace(Chr(13), "").Split(Chr(10))
I like doing it this way. 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.
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