Click here to Skip to main content
15,919,245 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
What I have is a list box loading a text file with something around 100 items sorted from A to Z and I would like to split them into different columns

Column 1 A-F
Column 2 J-R
Column 3 S-Z

What I would like to know is if this is possible and if so how would I start I'm not able to find any info on this.

What I have tried:

I have not tried much I have no idea where to start or if it's even possible and all my searches are turning up empty due to the fact it's a very specific and I'm guessing uncommon question?
Posted
Updated 1-Aug-16 15:39pm
Comments
Garth J Lancaster 1-Aug-16 21:15pm    
I think you need to provide a definition of 'items' and 'what the key is' for the 'A' to 'Z' - for example, are items an 'object' where 'key' is defined as perhaps a property or, are items simply a string and the key 'A'..'Z' comes from the first or 'n-th' position in the string ?

(please use 'improve question' to add this information to your question, rather than reply back to this - doing so means others can perhaps help)
Karthik_Mahalingam 2-Aug-16 11:01am    
give some sample data of the textfile

1 solution

I'm going to give you a thought on a way to start thinking, even though your requirements (as Ive commented) aren't 100% defined - to do this without better requirements may be wrong, but may help you see the wood for the forest

lets say you have an array of strings, that array being all the lines in your datafile

C#
string path = @"C:\some dir\somefile.txt";
string[] allFileLines = File.ReadLines(path)


yes ?

so for column 1 you want to get matching 'lines' from allFileLines where some 'key' falls in the range A-F .. so, at a very basic level, using LINQ,

C#
var column1Lines = 
  from line in allFileLines
  where (some condition that matches 'line' to key 'A-F')
  select line;


and therefore repeated for column2 J-R, column3 S-Z. Now, there may be many ways of optimising this, and, I could with a bit of thought return a dictionary with a string key 'column1' -> list of lines for column1, but, maybe thats simply not required/OTT

This bit (some condition that matches 'line' to key 'A-F') really becomes crucial according to the comment I posted - it could be a Regex Match, a separate function taking 'A-F' as input and performing your match, or anything ..

.. but for us to be able to give you a solution, it really needs a better 'spec'/requirement definition on your part - once you've done that, you could use LINQ, or you could use a foreach on allFileLines with a 'classifier' function and a switch to add the 'classified line' to a list for column 1, 2 or 3
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900