Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a text file contains data in the format

100;10:24
100;11:34
200;9:40
200;12:45

how I can convert the data after reading to the format

100 10:24 11:34
200 9:40 12:45


as result I want to combine all line starts with 100 in single line 100 is a key and starts with 200 in line so the first column is a key
Posted

Create a Dictionary<int, string >() object
Open the file
for each line use "Split" to split on ";" which gives an array of strings
Convert the first item in the array (the 100, 200 etc) to an int
Check if that int exists as a key in your dictionary (use ContainsKey)
if the key doesn't exist, add it with the value being the second item in the array
if the key does exist, get the associated string and append a space and then the second item in the array

Create a new file
Now loop through each key item in the dictionary and write out the key plus a space plus the string value


To pre-empt your next question, no I won't "give you the code", it should be simple enough to convert the pseudo code above into actual code.
 
Share this answer
 
Comments
Frankie-C 1-Jun-15 6:22am    
+5
you can use LINQ

VB
Dim lines = File.ReadAllLines(filepath).Select(Function(record) record.Split(";"C)).Where(Function(cell) cell.Length >= 2).GroupBy(Function(x) x(0)).Select(Function(x) x.Key + " " + String.Join(" ", x.Select(Function(y) y(1))))

File.WriteAllLines(filepath, lines)


reference to read:
https://msdn.microsoft.com/en-us/library/system.io.file.readalllines(v=vs.110).aspx[^]
https://msdn.microsoft.com/en-us/library/system.io.file.writealllines(v=vs.110).aspx[^]
https://msdn.microsoft.com/en-us/library/b873y76a(v=vs.110).aspx[^]
https://msdn.microsoft.com/en-us/library/tabh47cf(v=vs.110).aspx[^]
https://msdn.microsoft.com/en-us/library/57a79xd0(v=vs.110).aspx[^]
https://msdn.microsoft.com/en-us/library/bb531412.aspx[^]
 
Share this answer
 
v2
 
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