Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have text file text.txt and numbers

1 2 54
2 4 4
32 4 6
3 16 6
3 53 7
2 6 92
12 2 31

line1: 1,2,32,3,3,2,12 string[0]
line2: 2,4,4,16,53,6,2 string[1]
line3: 54,4,6,6,7,92,31 string[2]

I want to put each line in a string(numbers split with space)
Please help!
Posted

1 solution

Do you mean that your file contains 7 lines of 3 numbers, or three lines of seven numbers?
Either way the principle is the same (I will assume your file contains N lines of three numbers, and you want three lines of N numbers as output). But, as this smells of homework, I won't give you code...
1) Set up three StringBuilders - one for each output, and string based separator, initially an empty string
2) Read a line from the file
3) Split the line (using string.Split) on the space character
4) Check you have exactly three parts, if not report an error and stop.
5) Add each part to the appropriate stringbuilder, using AppendFormat("{0}{1}", separator, splitpart)
6) Set the separator to a comma
7) While there are still lines in the file, repeat from 2.
 
Share this answer
 
Comments
dax88 21-Nov-12 14:22pm    
and how do I put this in a while?
OriginalGriff 21-Nov-12 14:49pm    
You could always use a loop...
There are several kinds: foreach, for, while and do.
Depending on how you read your lines from the file, I would suggest either foreach or while would be the best.

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