forum

Home / DeveloperSection / Forums / How to use LINQ to separate a list element and inserting the results back to list

How to use LINQ to separate a list element and inserting the results back to list

Anonymous User 2057 26-Jan-2015

I have a list of strings like this(""Joe", "wants", "to", "thank", "you!"") what I want is to separate "you", "!" and """ and insert them back to that list using LINQ. I know there are many LINQ experts here, which can do it in a minute. What I'm doing is separating a sentence into words:

    string sentence = "\"Joe wants to thank you!\"";
    string[] words = sentence.split(" ");
    List<string> result = new List<string>();
 
    for (int i = 0; i < words.Length; i++)
    {
        string word = words[i];
        if (word.EndsWith("."))
        {
            result.Add(word.Substring(0, word.LastIndexOf(".")));
            result.Add(".");
        }
        else if (word.EndsWith("..."))
        {
            result.Add(word.Substring(0, word.LastIndexOf("...")));
            result.Add("...");
        }
        else if (word.EndsWith(","))
        {
            result.Add(word.Substring(0, word.LastIndexOf(",")));
            result.Add(",");
        }
        else if (word.EndsWith("\""))
        {
            result.Add(word.Substring(0, word.LastIndexOf("\"")));
            result.Add("\"");
        }
 
 
     }

The problem is with sentence that ends with !"NOTE: words is the array splitted by space.

 


Updated on 27-Jan-2015
I am a content writter !

Can you answer this question?


Answer

1 Answers

Liked By