嗨,任何人都可以解释这些代码行,我需要了解它是如何工作的,以便继续我正在做的事情
if (e.Error == null){
Stream responseStream = e.Result;
StreamReader responseReader = new StreamReader(responseStream);
string response = responseReader.ReadToEnd();
string[] split1 = Regex.Split(response, "},{");
List<string> pri1 = new List<string>(split1);
pri1.RemoveAt(0);
string last = pri1[pri1.Count() - 1];
pri1.Remove(last);
}
答案 0 :(得分:4)
// Check if there was no error
if (e.Error == null)
{
// Streams are a way to read/write information from/to somewhere
// without having to manage buffer allocation and such
Stream responseStream = e.Result;
// StreamReader is a class making it easier to read from a stream
StreamReader responseReader = new StreamReader(responseStream);
// read everything that was written to a stream and convert it to a string using
// the character encoding that was specified for the stream/reader.
string response = responseReader.ReadToEnd();
// create an array of the string by using "},{" as delimiter
// string.Split would be more efficient and more straightforward.
string[] split1 = Regex.Split(response, "},{");
// create a list of the array. Lists makes it easier to work with arrays
// since you do not have to move elements manually or take care of allocations
List<string> pri1 = new List<string>(split1);
pri1.RemoveAt(0);
// get the last item in the array. It would be more efficient to use .Length instead
// of Count()
string last = pri1[pri1.Count() - 1];
// remove the last item
pri1.Remove(last);
}
如果唯一要做的就是删除第一个和最后一个元素,我会使用LinkedList
而不是List
。
答案 1 :(得分:1)
它将响应流作为字符串读取,假设字符串由逗号分隔的序列“{...}”组成,例如:
{X},{Y},{Z}
然后将字符串拆分为“},{”,给出一个
数组{X
ý
Z}
然后从数组的第一个元素({X =&gt; X)中删除第一个大括号,从数组的最后一个元素(Z} =&gt; Z)中移除结束大括号。
答案 2 :(得分:0)
从我所看到的,它是从一个可能来自TCP的流中读取的。
它读取整个数据块,然后使用分隔符},{
分隔块。
因此,如果您有类似abc},{dec
的内容,它将被置于具有2个值的split1数组中,split1 [0] = abc,split1 [1] = dec。
之后,它基本上删除了第一个和最后一个内容
答案 3 :(得分:0)
正在处理错误输出。 它收到了来自e的流(我想这是一个例外),读取它。 它看起来像: “”{DDD},{我失败},{因为},{没有信号} {ENDCODE} 它将其拆分为不同的字符串,并删除到第一个和最后一个条目(DDD,ENDCODE)