dexter
dexter

Reputation: 93

Split data from multiple rows in excel using a delimiter

I've a windows form with multiline text box. I'm trying to copy paste some data from excel sheet

enter image description here

I'm trying to split this values and add it to a string array using the below code

string[] languageList = language.Split('\n');

The output I'm getting is a single string

"c#javac++C"

instead of 4 strings, ie

languageList[0] = "c#"
languageList[1] = "java"
languageList[2] = "c++"
languageList[3] = "C"

Is there a way to split the excel row values using any delimiter?

Upvotes: 0

Views: 1677

Answers (1)

thekaveman
thekaveman

Reputation: 4409

Your chosen delimiter \n is the Linefeed LF character, which delimits new lines on Linux-like operating systems.

The delimiter \r\n is the Carriage Return + Linefeed CRLF character, which delimits new lines on Windows machines.

For the most robust code, use the System.Environment.NewLine property, which will pick the correct delimiter string based on the environment.

string[] languageList = language.Split(new[] { System.Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);

Notice the use of new[] { System.Environment.NewLine } - this is because when using a string separator with String.Split() you must pass it as a string[] argument.

Upvotes: 1

Related Questions