如何将string []行更改为输出字符串 [英] How do I change string[] lines into output string
本文介绍了如何将string []行更改为输出字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何将string []行更改为输出字符串?
How do I change string[] lines into output string?
protected void Button1_Click(object sender, EventArgs e)
{
string data1 = TextBox1.Text;
using (System.IO.FileStream fs = new System.IO.FileStream(Server.MapPath("~/data.txt"), System.IO.FileMode.Append, System.IO.FileAccess.Write, System.IO.FileShare.Read, 8, System.IO.FileOptions.None))
{
byte[] data = System.Text.Encoding.ASCII.GetBytes(data1);
fs.Write(data, 0, data.Length);
fs.Flush();
fs.Close();
}
}
protected void Button2_Click(object sender, EventArgs e)
{
string[] lines = File.ReadAllLines(Server.MapPath("~/data.txt"));
string stglines
???change string[] to strlines
TextBox2.Text = strline.ToString();
}
推荐答案
为什么不使用 ReadAllText(String)
代替?
您需要做的就是使用 StringBuilder类 [ ^ ]。
All you need to do is to loop through the collection of lines using StringBuilder class[^].
StringBuilder sb = new StringBuilder();
foreach (string str in lines)
{
sb.Append(str);
}
//finally:
TexBox2.Text = sb.ToString();
但是,就个人而言,我建议使用解决方案1.
String.Concat()方法 [ ^ ]连接指定String数组的元素。
谢谢PIEBALDconsult。我忘了这个方法!
But, personally, i'd suggest to use solution 1.
String.Concat() method[^] concatenates the elements of a specified String array.
Thank you, PIEBALDconsult. I forgot about that method!
这篇关于如何将string []行更改为输出字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文