本文介绍了需要使用C#中的代码来分隔文本!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的文字:明天将对阵切尔西的阿森纳.

现在我要这样分开:

明天将播放
将参加阿森纳
对阵阿森纳
阿森纳对切尔西
对切​​尔西.

然后将它们分开,而不是将其放在列表框中!

I have a text like that " Tomorrow will play Arsenal against Chelsea.

Now I want to seperate like that:

Tomorrow will play
will play Arsenal
play Arsenal against
Arsenal against Chelsea
against Chelsea.

and after we seperate than put this in a listbox!

推荐答案


class Program
{
    static void Main(string[] args)
    {
        string data = "Tomorrow will play Arsenal against Chelsea";

        string[] arr = data.Split(new char[] { ' ' });

        int arrayPosition = 0;
        int length = 3;
        List<List<string>> result = new List<List<string>>();
        while (arrayPosition < arr.Length)
        {
            List<string> innerList = new List<string>();
            for (int i = arrayPosition, innerLoop = 0; innerLoop < length; ++i, ++innerLoop)
            {
                if (i < arr.Length)
                    innerList.Add(arr.ElementAt(i));
            }
            result.Add(innerList);
            ++arrayPosition;
        }
        result.ForEach(item =>
        {
            item.ForEach(innerItem => Console.Write("{0,10}",innerItem));
            Console.WriteLine();
        });


    }
}



希望对您有所帮助:)



Hope it helps :)


这篇关于需要使用C#中的代码来分隔文本!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-15 10:26