Linq Split String To List

Linq Split String To List

Linq Split String To List. Here we will see how to split string to list and display each words using Linq query. Example uses C#.net language.

  1. Linq Chapter 1 – Split string to list of strings.
  2. Linq Chapter 2 – Remove duplicates from list.
  3. Linq Chapter 3 – Linq Where Clause.
  4. Linq Chapter 4 – Linq Group by multiple clause.
  5. Linq Chapter 5 – Linq Inner Join query.
  6. Linq Chapter 6 – Linq Distinct list.
  7. Linq Chapter 7 – Linq OfType Method.

C# Linq Code get words in string

void Main()
{
    
    
    var inputStringArray = "This is Linq split string and get words example".Split(" ".ToCharArray());
    
    var words = from wordCollection in inputStringArray
    orderby wordCollection.Length descending
    select wordCollection;
    
    
    foreach(string word in words)
    
    {
     Console.WriteLine( word  );
    }
    
    Console.WriteLine("Total number of words: "+ words.Count());
    
}


Output

  example
  string
  split
  words
  This
  Linq
  and
  get
  is
  Total number of words: 9

We first split the string into an array at spaces and using Linq query we are collecting each words in descending order of it’s length.

Linq — How To Find words contained in string.

C# example uses Linq statement to find words contained in string.

void Main()
{
    
    
    var inputStringArray = "This is Linq Where Clause example ( Linq )".Split(" ".ToCharArray());
    
    var words = from wordCollection in inputStringArray
    where wordCollection == "Linq"
    select wordCollection;
    
    
    foreach(string word in words)
    
    {
     Console.WriteLine( word  );
    }
    
    Console.WriteLine("Total number of words: "+ words.Count());
    
}


Output
Linq
Linq
Total number of words: 2

Tags:

Linq Inner JoinSplit String In C#c# arraylistC# ArrayList Search using Linq Extension OfType MethodC# DateTime ToString()Linq Distinct Listlistbox to listboxLinq Group By Multiple ColumnsLinq Convert String Array To Int Array.net String vs stringAsp.net Convert String To Image

Author

My name is Satalaj, but people call me Sat. Here is my homepage: . I live in Pune, PN and work as a Software Engineer. I'm former MVP in ASP.net year 2010.
Disclaimer: Views or opinion expressed here are my personal research and it has nothing to do with my employer. You are free to use the code, ideas/hints in your projects. However, you should not copy and paste my original content to other web sites. Feel free to copy or extend the code.
If you want to fight with me, this website is not for you.
 

I'm Satalaj.