LINQ to Strings

LINQ to strings is nothing but writing LINQ queries on the string to get the required data from the string sequence. In LINQ, we can write queries on strings along with traditional string functions and regular expressions to perform the required operation on strings using LINQ.

Syntax of LINQ to Strings

Following is the syntax of writing LINQ queries on string collections.

 

C# Code

 

var result = from s in str.ToLowerInvariant().Split()

VB.NET Code

 

Dim result = From s In str.ToLowerInvariant().Split()

If you observe above syntax we written LINQ query on string to get distinct elements.

Example of LINQ to Strings

Following is the example of writing LINQ queries on the string to split a string into words in c#, vb.net

 

C# Code

 

using System;
using System.Linq;

namespace Linqtutorials
{
  class Program
  {
    static void Main(string[] args)
    {
      string str = "welcome to tutlane.com";
      var result = from s in str.ToLowerInvariant().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
                   select s;
      foreach (var item in result)
      {
        Console.WriteLine(item);
      }
      Console.ReadLine();
    }
  }
}

VB.NET Code

 

Module Module1

Sub Main()
Dim str As String = "welcome to tutlane.com"
Dim result = From s In str.ToLowerInvariant().Split(New Char() {" "}, StringSplitOptions.RemoveEmptyEntries)
For Each item In result
Console.WriteLine(item)
Next
Console.ReadLine()
End Sub
End Module

If you observe the above example, we wrote a simple query in LINQ to split string “str” into multiple words. Here, our “str” string object contains too many spaces between words to remove that one; we used the “StringSplitOptions.RemoveEmptyEntries” property. In case if we use traditional coding, we need to write a lot of code to split the string and remove spaces between words.

Result of LINQ to Strings Example

Following is the result of the LINQ to Strings example.

 

welcome
to
tutlane.com

This is how we can write LINQ queries on strings to get the required data from a string. We will see another example to split the string and get distinct words using a LINQ query.

LINQ to Strings Example2

Following is the example to find distinct words from a given string using LINQ queries in c#, vb.net

 

C# Code

 

using System;
using System.Linq;

namespace Linqtutorials
{
  class Program
  {
    static void Main(string[] args)
    {
      string str = "welcome welcome to to tutlane.com";
      var result = from s in str.ToLowerInvariant().Split().Distinct()
                   select s;
      foreach (var item in result)
      {
        Console.WriteLine(item);
      }
      Console.ReadLine();
    }
  }
}

VB.NET Code

 

Module Module1

Sub Main()
Dim str As String = "welcome welcome to to tutlane.com"
Dim result = From s In str.ToLowerInvariant().Split().Distinct()
For Each item In result
Console.WriteLine(item)
Next
Console.ReadLine()
End Sub
End Module

If you observe the above example, we split the string and applied distinct property to get distinct words from the string.

Result of LINQ to Strings Example

Following is the result of the LINQ to Strings example to split and get distinct words from a string.

 

welcome
to
tutlane.com

This is how we can write LINQ queries to transform strings and collection of strings in c#, vb.net.