需要使用C#中的代码来分隔文本! [英] Need code in c#, to seperate text !
本文介绍了需要使用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!
推荐答案
使用循环和Split().
Use a loop and Split(). That should get you by.
我假设逻辑是每三个单词需要从头到尾进行分组,即.从第一个单词(明天)到3个单词(直到比赛),然后从第二个单词(将直到3个单词(直到阿森纳),依此类推.
如果我是对的,那么遵循以下代码可能会有所帮助,
I am assuming the logic is every three words needs to group from the start to end ie. from the first word (Tomorrow) to until 3 words(till play), then second word(will) to until 3 words(till Arsenal) and so on.
If I am right probably following code will help,
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#中的代码来分隔文本!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文