解释代码

发布于 2024-11-09 17:33:07 字数 454 浏览 0 评论 0原文

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

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);
}

Hi can anyone explain these lines of codes, I need to understand how it works in order to proceed with what I am doing

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);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

短暂陪伴 2024-11-16 17:33:07
// 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

// 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);
     }

I would use a LinkedList instead of List if the only thing to do was to remove the first and last elements.

瑾兮 2024-11-16 17:33:07

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

{X},{Y},{Z},

然后将字符串拆分为“}, 数组

{",给出一个{X

Y

Z}

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

It's reading the response stream as a string, making the assumption that the string consists of sequences "{...}" separated by commas, e.g.:

{X},{Y},{Z}

then splits the string on "},{", giving an array of

{X

Y

Z}

then removes the first brace from the first element of the array ( {X => X ) and the end brace from the last element of the array ( Z} => Z).

饮湿 2024-11-16 17:33:07

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

因此,如果您有类似 abc},{dec 的内容,它将被放入包含 2 个值的 split1 数组中, split1 [0]=abc 、 split1 [1]=dec 。

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

From what I can see, it is reading from a stream that could have came from TCP.
It reads the whole chunk of data, then separate the chunk using the delimiter },{.

So if you have something like abc},{dec , it will be placed into split1 array with 2 values, split1 [0]=abc , split1 [1]=dec.

After that, it basically remove the 1st and the last content

无所谓啦 2024-11-16 17:33:07

它正在处理错误输出。
它从 e 接收到一个流(我猜这是一个例外),读取它。
它看起来像:
""{DDD},{我失败了},{因为},{没有信号}{ENDCODE}
它将它分成不同的字符串,并删除第一个和最后一个条目(DDD,ENDCODE)

It is processing an error output.
It received a stream from the e (I guess it is an exception), reads it.
It looks something like :
""{DDD},{I failed},{Because},{There was no signal}{ENDCODE}
It splits it into different string, and removes to fist and last entries (DDD, ENDCODE)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文