Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to split new line in string in vb.net

for example .. If I have a text like this

214asd
df5df8
d66f66

I want to split them into 3 strings using vb.net .

like image 559
Adham Avatar asked Feb 10 '13 08:02

Adham


People also ask

How do you split a string into a new line in VB?

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.

How do you break a string to the next line?

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.

How Split Comma Separated Values in VB net?

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 SubString in VB net?

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.


2 Answers

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.

like image 125
Oded Avatar answered Oct 10 '22 09:10

Oded


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.

like image 22
Cory Jackson Avatar answered Oct 10 '22 09:10

Cory Jackson