Click here to Skip to main content
11,930,788 members (54,603 online)
Rate this:
Please Sign up or sign in to vote.
See more: C#
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 21-Nov-12 8:41am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.151126.1 | Last Updated 21 Nov 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100