CPK_2011
CPK_2011

Reputation: 1150

Text File delimiting - Performance issue

I have a text file with details as follows with no headers

 Name1 Text1 This is the message1
 Name2 Text2 This is the message2

If I use like this..

string[] allLines = File.ReadAllLines("TextFile.log");
for (int i = 0; i < allLines.Length; i++
{
    string[] items = allLines[i].Split(new char[] { ' ' });
    MessageBox.Show("This is Name field : " + items[0])      
    MessageBox.Show("This is Text field : " + items[1])      
    MessageBox.Show("This is Message field : " + items[2])      
}

If I use the above code, it will work fine for first 2 fields but how can I get the third column "This is the message1" in single column?

Upvotes: 2

Views: 61

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1039368

Just specify that you want at most 3 items when splitting using the appropriate overload of the Split method:

string[] items = allLines[i].Split(new char[] { ' ' }, 3);

Upvotes: 8

Related Questions