Joe handy Joe handy - 2 months ago 6
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:

CurrentVersion: 1.0.0.0
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: 1.0.0.0


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))
interpret(myString);


And when I call

MessageBox.Show(myString);


I get 2 MessageBoxes. The first MessageBox displays:

CurrentVersion: 1.0.0.0


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
string[] lines = TextDocument.Text.Split(new string[] 
    { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);

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