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.
Following is the syntax of writing LINQ queries on string collections.
C# Code
VB.NET Code
If you observe above syntax we written LINQ query on string to get distinct elements.
Following is the example of writing LINQ queries on the string to split a string into words in c#, vb.net
C# Code
VB.NET Code
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.
Following is the result of the LINQ to Strings example.
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.
Following is the example to find distinct words from a given string using LINQ queries in c#, vb.net
C# Code
VB.NET Code
If you observe the above example, we split the string and applied distinct property to get distinct words from the string.
Following is the result of the LINQ to Strings example to split and get distinct words from a string.
This is how we can write LINQ queries to transform strings and collection of strings in c#, vb.net.