Help trying to split words to a list?

0 jhonathan macy · May 2, 2015
Hello, so i am trying to get words that the user inputs and add them to a list, for example:


help stop these are, the, words that i, want

The first two words will not matter to me, but the words after that i will want to add to a list and the commas would help put them in there so that is would be like this:


List Words = new List();
Words[0] = these are
Words[1] = the
Words[2] = words that i
Words[3] = want



Now i tried writing one to do this, but my output keeps giving me this(example):


these are
these the
these words that i
these words that want
these words that
these words
these

How can i fix this? here is my code:


public static void testing(string[] str) {
            List CurWords = new List();
            
            
            int i = 0;
            foreach (string x in str)
            {
                if (x != str[0] || x != str[1])
                {
                    if (x.Contains(","))
                    {
                        int a = x.IndexOf(",");
                        string newSTR = x.Remove(a);
                        if (tryTheError(i, CurWords))
                        {
                            CurWords.Insert(i, string.Concat(string.Concat(CurWords[i], " "), newSTR));
                        }
                        else {
                            CurWords.Add(newSTR);
                        }
                        i++;
                    }
                    else {
                        
                        if (tryTheError(i, CurWords))
                        {
                            CurWords.Insert(i, string.Concat(string.Concat(CurWords[i]," "), x));

                        }
                        else
                        {
                            CurWords.Add(x);
                        }
                    }
                }
                
            }
            if (CurWords.Count != 0) {
                foreach (string a in CurWords) {
                    Console.WriteLine(a);
                }
            }
            
        }
        //for checking if CurWords[i] exist
        public static bool tryTheError(int i, List Words) {
            try
            {
                int test = Words[i].Count();
            }
            catch{
                return false;
            }
            return true;
        }

Thank you for any help.

Post a Reply

Replies

Oldest  Newest  Rating
0 Alan Johnson · May 31, 2015
Hi sorry this is a late response, I wrote a method to do this for you.


        public List WordHelper()
        {
            // Enter the words to break apart - e.g "help stop these are, the, words that i, want"
            Console.WriteLine("Enter words to split up: ");
            var Input = Console.ReadLine();

            // Get rid of the first two words as you don't want those
            string[] Words = Input.Split(' ').Where((value, id) => id >= 2).ToArray();

            // Now break the words you want apart by commas
            var ReturnedWords = string.Join(" ", Words).Split(',').ToList();

            // Print each word out
            foreach (var Word in ReturnedWords)
            {
                Console.WriteLine(Word.Trim());
            }

            // Finally return them as a List
            return ReturnedWords;
        }
0 Bill Harding · May 28, 2015
Try a step by step approach.

start with this string (it's not an array)
help stop these are, the, words that i, want

remove reserved words with replace.
  these are, the, words that i, want

trim the string
these are, the, words that i, want

use split with , as the character to split on
these are
the
words that i
want

trim each item if needed.
  • 1

C#

107,328 followers
About

C# is an object-oriented language designed for improving the development of web applications.

Links
Moderators