The Muffin Man
The Muffin Man

Reputation: 20004

Is this the correct way to split a string using line breaks

I've written some code to take a word doc and copy it to the clipboard. In this word doc there is 800+ strings on their own line.

I'm trying to split the document by line, insert it into a list and then for testing purposes display one of the lines. However, I am getting an empty message box. (Previous tests show the list does indeed contain 800+ rows. They may just be null because of wrong code.)

Here's my code:

string myData = data.GetData(DataFormats.Text).ToString();

List<string> myList = new List<string>(myData.Split(new char[]{'\r','\n'}));

MessageBox.Show(myList[5]);

What am I doing wrong?

Upvotes: 0

Views: 227

Answers (1)

Bala R
Bala R

Reputation: 108957

try

myData.Split(new string[]{Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries)

Upvotes: 5

Related Questions