---
title: "How to use LINQ to separate a list element and inserting the results back to list"  
description: "How to use LINQ to separate a list element and inserting the results back to list"  
author: "Anonymous User"  
published: 2015-01-26  
updated: 2015-01-27  
canonical: https://www.mindstick.com/forum/12900/how-to-use-linq-to-separate-a-list-element-and-inserting-the-results-back-to-list  
category: "asp.net"  
tags: ["c#", "linq", "listview"]  
reading_time: 2 minutes  

---

# How to use LINQ to separate a list element and inserting the results back to list

I have a list of [strings](https://www.mindstick.com/forum/161912/explain-the-python-strings) like this("**"Joe**", "**wants**", "**to**", "**thank**", "**you!"**") what I want is to separate "you", "!" and "**"**" and [insert](https://www.mindstick.com/blog/173/executing-insert-delete-or-update-query-in-sqlserver-using-ado-dot-net) them back to that list using [LINQ](https://www.mindstick.com/articles/12007/language-integrated-query-linq-queries). 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](https://yourviews.mindstick.com/view/81399/tackling-the-problem-of-unemployment-during-corona-pandemic) is with sentence that ends with **!"**. **NOTE: words is the [array](https://www.mindstick.com/articles/335/jagged-array-in-c-sharp-dot-net) splitted by space.**

## Replies

### Reply by Anonymous User

since you are already splitting it by spaces, just [replace](http://msdn.microsoft.com/en-us/library/fk49wtc1%28v=vs.110%29.aspx) and add a space before splitting up

sentence = sentence.Replace("!", " !");

I don't think you need linq but something kind of elegant for you would be

```
var addMyspace = new List<string>{"!", "...", "\"", ".", ","}; foreach(var s in addMyspace){     sentence = sentence.Replace(s, string.Format(" {0}",s));}//split
```


---

Original Source: https://www.mindstick.com/forum/12900/how-to-use-linq-to-separate-a-list-element-and-inserting-the-results-back-to-list

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
