The answer from Brian Watts is elegant and simple. It implicitly refers to an array of strings created by Split ().
Also pay attention to its extensibility if you are reading a file and want to massage the data when building the array.
string sFileA = @"C:\Documents and Settings\FileA.txt"; string sFileB = @"C:\Documents and Settings\FileB.txt"; // Trim extraneous spaces from the first file data string[] fileAData = (from line in File.ReadAllLines( sFileA ) select line.Trim()).ToArray(); // Strip a second unneeded column from the second file data string[] fileBData = (from line in File.ReadAllLines( sFileB ) select line.Substring( 0, 21 ).Trim()).ToArray();
Of course, you can use the Linq => notation if you want.
string[] fileBData = File.ReadAllLines( sFileB ).Select( line => line.Substring( 0, 21 ).Trim()).ToArray();
Although my answer should have been posted as a comment, I have no comments for comments yet. But I found this discussion invaluable in determining how to massage data using ReadAllLines ().
RonSanderson Mar 17 '16 at 20:33 2016-03-17 20:33
source share