Visual C#数组和字符串 [英] Visual c# Arrays and string

查看:96
本文介绍了Visual C#数组和字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有文本文件text.txt和数字

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

第1行:1、2、32、3、3、2、12字符串[0]
第2行:2,4,4,16,53,6,2字符串[1]
第3行:54、4、6、6、7、92、31字符串[2]

我想将每一行放在一个字符串中(数字用空格分开)
请帮忙!

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!

推荐答案

您是说文件包含7行3个数字还是3行7个数字?
两种方法的原理都是相同的(我假设您的文件包含N行的3个数字,而您希望输出3行的N个数字).但是,由于这是作业的味道,我不会给您代码...
1)设置三个StringBuilder-每个输出一个,以及基于字符串的分隔符,最初是一个空字符串
2)从文件中读取一行
3)在空格字符上分割行(使用string.Split)
4)检查您是否完全由三个部分组成,如果没有报告错误,请停止.
5)使用AppendFormat("{0} {1}",分隔符,splitpart)将每个部分添加到适当的stringbuilder中.
6)将分隔符设置为逗号
7)文件中仍然有行时,请从2开始重复.
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.


这篇关于Visual C#数组和字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆