LINQ to Strings

  Total Views : 122
  Comments : 0
 
  Keywords : Linq to strings with example in c# / vb.net, How to use linq queries on strings to get data in c# / vb.net with example, Linq queries to transform strings and collection of strings in c#

Here we will learn LINQ to Strings in c#, vb.net with example and how to use LINQ to Strings to split string into words, find distinct words from string with examples in c#, vb.net.

LINQ to Strings

LINQ to strings is nothing but writing LINQ queries on string to get required data from string sequence. In LINQ we can write queries on strings along with traditional string functions and regular expressions to perform 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 string to split 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() {" "c}, StringSplitOptions.RemoveEmptyEntries)

For Each item In result

Console.WriteLine(item)

Next

Console.ReadLine()

End Sub

End Module

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

Output of LINQ to Strings Example

Following is the result of LINQ to Strings example.

 

welcome

to

tutlane.com

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

LINQ to Strings Example2

Following is the example to find distinct words from 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 above example, we splitted string and applied distinct property to get distinct words from the string.

Output of LINQ to Strings Example

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

 

welcome

to

tutlane.com

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

 

PREVIOUS

LINQ to Objects
 

 Comments (0)

Be the first to give your valuable feedback

Leave a comment

  • Default Tutlane User