Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
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!
Posted
Comments
Sergey Alexandrovich Kryukov 16-Apr-12 17:19pm    
Not enough information. Start with your purpose: why do you need it? Think if the problem could be solved is some different ways. What are the criteria for the split? Use "Improve answer" above.
--SA
h7h7h7 16-Apr-12 18:21pm    
I am sory for les info. So I have file data.txt (with a text inside)...I have a code to make array with double words....ex "tomorrow is god day", my code do that: tomorrow is
is god
god day

and the code is:
string text= File.ReadAllText(textbox1.Text,System.Text.Encoding.Default);



List<string> word= new List<string>(textbox1.Split(' '));

word.Remove("");




List<string[]> double= new List<string[]>();

for (int i = 0; i < word.Count; i++)
{
if (i + 1 < word.Count)
double.Add(new string[] { word[i], word[i + 1] });
else
double.Add(new string[] { word[i], ""});
}

// listbox1.Items.Clear();
foreach (string[] doub in double)
{
listbox1.Items.Add(doub[0] + " " + doub[1]);
}

I don't know hot to make with three word:
tomorrow is god
is god day.
Sergey Alexandrovich Kryukov 16-Apr-12 18:43pm    
You still did not describe the rules and did not answer why doing that.
--SA
[no name] 16-Apr-12 17:24pm    
Have you searched or Googled for a solution?
Be specific! Don't ask "I need to write a booking application". Specify exactly what it is you need help with.
Keep the subject brief but descriptive. eg "How do I change the dialog colour?"
If you have a school or university assignment, assume that your teacher or lecturer is also reading these forums.

Use a loop and Split(). That should get you by.
 
Share this answer
 
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,

C#
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 :)
 
Share this answer
 
v3
Comments
h7h7h7 17-Apr-12 3:14am    
Mohammad in this part of code "result.Add(innerList);" there is an error ??!
I have tried to solve but I cant't ?? ande here too " innerList.Add(arr.ElementAt(i))"
Mohammad A Rahman 17-Apr-12 3:52am    
Try now with updated code :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900