首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >解释代码

问解释代码
EN

Stack Overflow用户
提问于 2011-05-25 15:22:24
回答 4查看 99关注 0票数 0

嗨,谁能解释这些代码行,我需要了解它是如何工作的,以便继续我正在做的事情

代码语言:javascript
复制
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);
}
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2011-05-25 15:34:15

代码语言:javascript
复制
// 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。

票数 4
EN

Stack Overflow用户

发布于 2011-05-25 15:30:37

它将响应流作为字符串读取,假设字符串由逗号分隔的序列"{...}“组成,例如:

{X}、{Y}、{Z}

然后在"},{“上拆分字符串,给出一个

{X

是

Z}

然后从数组的第一个元素( {X => X)中删除第一个大括号,并从数组的最后一个元素( Z} => Z)中删除最后一个大括号。

票数 1
EN

Stack Overflow用户

发布于 2011-05-25 15:28:25

据我所见,它正在从可能来自TCP的流中读取数据。它读取整个数据块,然后使用分隔符},{分隔这些块。

所以如果你有像abc},{dec这样的东西,它将被放入带有2个值的split1数组中,split1 =abc,split1 1=dec。

在那之后,它基本上删除了第一个和最后一个内容

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6120882

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档