none
item.Split not working RRS feed

  • Question

  • I want to take the info from a listbox, remove unwanted words, and then write a coma separated row.

    public void saveListButton_Click(object sender, EventArgs e)
            {
                const string sPath = (@"C:\Users\user\Desktop\Document.TXT");
    
                System.IO.StreamWriter SaveFile = new System.IO.StreamWriter(sPath);
                foreach (var item in usersListBox.Items)
                {
                    item.ToString().Replace(" ", ",");
                    List<string> unwantedWords = new List<string> { "is", "a", "of" };
                    var linesSplitted = item.Split(",").ToList();
                    linesSplitted.Where(i => !unwantedWords.Contains(i)).ToList();
                    var wordsWithoutUnwantedWords = linesSplitted.Where(i => !unwantedWords.Contains(i)).ToList();
                    for (int i = 0; i < wordsWithoutUnwantedWords.Count; i++)
                    {
                        var isLastWord = i == wordsWithoutUnwantedWords.Count - 1;
    
                        Console.Write(wordsWithoutUnwantedWords[i]);
    
                        if (!isLastWord)
                        {
                            Console.Write(",");
                        }
                    }
    
    
                }
    
                SaveFile.Close();
    
                MessageBox.Show("Programs saved!");
            }

    But I get an error on this line:

    var linesSplitted = item.Split(",").ToList();

    Any suggesttions? Thanks!

    Saturday, October 19, 2019 8:38 AM

Answers


  • But I get an error on this line:

    var linesSplitted = item.Split(",").ToList();

    Try this:

    var linesSplitted = item.ToString().Split(',').ToList();

    - Wayne

    • Marked as answer by horatiu_alex Saturday, October 19, 2019 10:36 AM
    Saturday, October 19, 2019 9:06 AM

All replies


  • But I get an error on this line:

    var linesSplitted = item.Split(",").ToList();

    Try this:

    var linesSplitted = item.ToString().Split(',').ToList();

    - Wayne

    • Marked as answer by horatiu_alex Saturday, October 19, 2019 10:36 AM
    Saturday, October 19, 2019 9:06 AM
  • I have tried this but I get an error for ToList()-fixed it thanks!
    • Edited by horatiu_alex Saturday, October 19, 2019 10:36 AM
    Saturday, October 19, 2019 10:30 AM