我的代码使用StringBuilder从网络流构建一个字符串。
然后我将生成的StringBuilder(myCompleatMessage)转换为ToString
的字符串。
此时字符串很好。
然后我尝试使用.Split()
分隔'〜'
当我尝试用控制台显示结果字符串或将其写入文件时,我得到:“System.String []”一遍又一遍地重复。
我的代码是:
byte[] myReadBuffer = new byte[3000];
StringBuilder myCompleteMessage = new StringBuilder();
int numberOfBytesRead = 0;
char[] separatingChars = {'~'};
// Incoming message may be larger than the buffer size.
do
{
numberOfBytesRead = stream.Read(myReadBuffer, 0, myReadBuffer.Length);
myCompleteMessage.AppendFormat("{0}", Encoding.ASCII.GetString(myReadBuffer, 0, numberOfBytesRead));
}
while (stream.DataAvailable);
string tosep = myCompleteMessage.ToString();
string[] words = tosep.Split(separatingChars);
Console.Write(tosep);
StreamWriter file = File.AppendText("c:\\Users/edegraaf/Desktop/test.txt");
foreach(string i in words)
{
file.WriteLine(words);
}
file.Close();
string sample:
24.062C 2017-05-31 19:36:20.143767~24.125C 2017-05-31 19:37:21.075028~30.25C 2017-05-31 19:38:21.953599~23.937C 2017-05-31 19:47:34.447627~23.937C 2017-05-31 19:47:52.717755~23.812C 2017-05-31 19:49:27.846434~24.0C 2017-05-31 20:01:08.865726~24.125C 2017-06-01 14:38:52.948025~23.375C 2017-06-01 14:39:53.872969~23.187C 2017-06-01 14:41:05.187961~28.625C 2017-06-01 15:01:48.22
谁能告诉我为什么我会收到“System.String[]
”?
答案 0 :(得分:5)
您尝试使用以下方法将数组本身写入文件,而不是写入数组的值:
file.WriteLine(words);
虽然你需要做的是,为了写下这些值:
foreach(string i in words)
{
file.WriteLine(i);
}
答案 1 :(得分:0)
将file.writeline(字)更改为file.writeline(i)。它的工作原理