15,845,436 members
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

## Solution 1

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.

Frankie-C 1-Jun-15 6:22am
+5

## Solution 3

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)```

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[^]

v2