Joe handy Joe handy - 1 year ago 41
C# Question

Save each line to a string from a text document

I'm trying to seperate each line from a text document and then save them to seperate strings. This is my text document:

Update information: New GUI, Fixed login system.

I have 2 strings declared in my program.

string CurrentVersion = String.Empty;
string UpdateInfo = String.Empty;

I want the string CurrentVersion to hold the first line. So:

CurrentVersion = CurrentVersion:

And I want UpdateInfo to hold the second line. So:

UpdateInfo = Update information: New GUI, Fixed login system.

I've tried this code:

foreach(var myString in TextDocument.Text.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries))

And when I call


I get 2 MessageBoxes. The first MessageBox displays:


And the second MessageBox displays

Update information: New GUI, Fixed login system.

But I want CurrentVersion and UpdateInfo to equal the lines. I'm not sure how to do this.

Answer Source
string[] lines = TextDocument.Text.Split(new string[] 
    { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);

string CurrentVersion = lines[0];
string UpdateInfo = lines[1];