Streamwriter writeline,但将每一行限制为一定数量的字符 [英] Streamwriter writeline but limit each line to a certain number of characters

查看:219
本文介绍了Streamwriter writeline,但将每一行限制为一定数量的字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,
我将如何使用Streamwriter编写具有固定数量的元素的行

之间有一个空格 例如,这里是一个要打开的文本文件:
MKWVTFISLLLLFSSAYSRGESHAGCEKSLHTLFGDELCKYLYEIAR
会显示为:


MKWVTFISLL LLLFSSAYSR
ESHAGCEKSL HTLFGDELCK
YLYEIAR

到目前为止我已经

字符串fileContents = File.ReadAllText(file,Encoding.ASCII); //序列数据库
字符串行=";
char [] sequenceCount = fileContents.ToCharArray();
int last = sequenceCount.Length;

for(int i = 0; i< 10; i ++)
{
行+ = Convert.ToString(sequenceCount [i]);
}

Hello,
How would I use the streamwriter to write a line with a fixed number of elements
and a space in between
For example here would be a text file to be opened:
MKWVTFISLLLLFSSAYSRGESHAGCEKSLHTLFGDELCKYLYEIAR
would read:


MKWVTFISLL LLLFSSAYSR
ESHAGCEKSL HTLFGDELCK
YLYEIAR

I have so far

string fileContents = File.ReadAllText(file, Encoding.ASCII); //Sequence database
string line = "";
char[] sequenceCount = fileContents.ToCharArray();
int last = sequenceCount.Length;

for (int i = 0; i < 10; i++)
{
line += Convert.ToString(sequenceCount[i]);
}

推荐答案

不,您没有解释要编写的内容.线边界怎么办?将行的末尾放在哪里?在哪里放置空格?所有这些活动的意义是什么?

让我们暂时忘记它.
这不是人们编写工作代码的方式.

不要立即常量.特别是,请勿使用",而应使用string.Empty.不要使用立即常量"10",而应将其用作构造函数或显式常量的某些功能的参数.

您不需要ToCharArray,这没有任何意义,因为sequenceCount[i]可以写为fileContents[i].

没有什么比循环中的字符串连接运算符"+"更糟糕的性能了.这是因为字符串不可变.当您将某些内容附加到字符串时,将创建一个全新的字符串对象,并将新的引用分配给赋值运算符左侧的变量. Is每次以不断增长的字符串内容调用一个字符串操作时,都会导致整个字符串的复制.这是非常无效的.您需要使用System.Text.StringBuilder及其方法AppendAppendFormat.请参阅 http://msdn.microsoft.com/zh-我们/library/system.text.stringbuilder(v=VS.100).aspx [
No, you did not explain what do you want to write. What to do with line boundaries? Where to put ends of line on output? Where to put blank space? What is the sense of all this activity?

Let''s forget it for now.
This is not how people write working code.

Do not immediate constants. In particular, don''t use "", use string.Empty. Don''t use immediate constant "10", make it a parameter of some function of constructor or explicit constant.

You don''t need ToCharArray, it makes no sense, because sequenceCount[i] can be written as fileContents[i].

Nothing is so bad for performance as string concatenation operator "+" in a loop. This is because strings are immutable. When you append something to string, a brand new string object is created and the new reference is assigned to the variable on left of the assignment operator. Is causes copy of the whole string every time a string operation is called with ever growing string contents. This is very ineffective. You need to use System.Text.StringBuilder and its method Append or AppendFormat instead. See http://msdn.microsoft.com/en-us/library/system.text.stringbuilder(v=VS.100).aspx[^].

The method Convert.ToString makes no sense. A character can be appended to a string. From the previous paragraph you can see that you should use System.Text.StringBuilder.Append which also accepts character.

Should I continue? …Keep trying.
Any follow-up questions are welcome.

—SA


这篇关于Streamwriter writeline,但将每一行限制为一定数量的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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