简体   繁体   English

从某个索引位置将字符串连接在一起 C#

[英]Joining strings together from certain index position C#

My application is reading a line from a text file and then saving this information into my Message class.我的应用程序正在从文本文件中读取一行,然后将此信息保存到我的 Message 类中。

I've written the following code, but for the next line in the text file, the message content is longer and needs to be something like split[2] + " " + split[3] + " " + split[4] + " " + split[5] to save the whole message contents.我已经编写了以下代码,但是对于文本文件中的下一行,消息内容较长,需要类似于 split[2] + " " + split[3] + " " + split[4] + " " + split[5] 保存整个消息内容。

public Conversation ReadConversation(string inputFilePath)
        {
            try
            {
                var reader = new StreamReader(new FileStream(inputFilePath, FileMode.Open, FileAccess.Read),
                    Encoding.ASCII);

                string conversationName = reader.ReadLine();
                var messages = new List<Message>();

                string line;

                while ((line = reader.ReadLine()) != null)
                {
                    var split = line.Split(' ');

                    string messageContent = split[2] + " " + split[3];


                    messages.Add(new Message(DateTimeOffset.FromUnixTimeSeconds(Convert.ToInt64(split[0])), split[1], messageContent));

                }

                return new Conversation(conversationName, messages);

As the length of each content varies, I've tried using string messageContent = string.Join(" ", split).由于每个内容的长度不同,我尝试使用 string messageContent = string.Join(" ", split)。

This adds all of the strings together but I need it starting from split[2] and to keep adding the remaining splits for the length of that particular line.这将所有字符串加在一起,但我需要它从 split[2] 开始,并为该特定行的长度继续添加剩余的拆分。 Is this possible?这可能吗?

I've tried using string.Join(" ", split, 2, split.count()) with 2 representing the index position I want to start from and count based on the total splits, but I get an error saying: System.ArgumentOutOfRangeException: 'Index and count must refer to a location within the buffer.我尝试使用 string.Join(" ", split, 2, split.count()) ,其中 2 表示我想要开始的索引位置并根据总拆分进行计数,但我收到一条错误消息:System. ArgumentOutOfRangeException: '索引和计数必须引用缓冲区内的一个位置。 (Parameter 'startIndex')' (参数'startIndex')'

Meaning of string.Join(" ", split, 2, split.count()) this line is read split collection then from starting position 2 join all split collection. string.Join(" ", split, 2, split.count())这一行是读取拆分集合,然后从起始位置 2 加入所有拆分集合。 For example, if you have string "Hello hi my name is".例如,如果您有字符串“你好,我的名字是”。 After splitting it's count will be 5 and starting from position 2 it will try to join all 5. This is why you are getting exception.拆分后,它的计数将为 5,并且从位置 2 开始,它将尝试加入所有 5 个。这就是您遇到异常的原因。 Instead you can try this string.Join(" ",split,2,split.Count()-2) .相反,你可以试试这个string.Join(" ",split,2,split.Count()-2) Starting from position 2, join all item from collection.从位置 2 开始,加入集合中的所有项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM