Jump to content

Check out our Community Blogs

Register and join over 40,000 other developers!

Recent Status Updates

View All Updates

- - - - -

c# linq with strings

ascii value string

  • Please log in to reply
No replies to this topic

#1 chili5


    CC Mentor

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3038 posts
  • Programming Language:Java, C#, PHP, JavaScript, Ruby, Transact-SQL
  • Learning:C, Java, C++, C#, PHP, JavaScript, Ruby, Transact-SQL, Assembly, Scheme, Haskell, Others

Posted 20 August 2011 - 07:44 AM

C# LINQ with Strings

We previously looked at various string methods. Let us now look at using LINQ with strings to perform some of the same operations and more. We will find these methods are mostly useful for searching and sorting strings. If you are familiar with joins and groups you should note it is possible to group characters and strings and you can join strings. If you are not familiar with LINQ and lambda functions start here.
A lot of these situations are not the best way to perform these operations and in fact are way more inefficient.

String Length Using LINQ

First, we need to create a string the same way as before:

string s = "welcome to codecall";
Now let us look at the LINQ query:

var lengthQuery = (from c in s
                   select c);

This is saying for each character in s called c select c as part of the results. When we execute this query we will have an IEnumerable collection of all the characters in c. Then to get the number of characters in s we simply call Count() on lengthQuery like this:


What happens here is the result set is enumerated and the numbers are characters are counted. While this works you should not ever count the characters in a string like this. By the time we written the query we have not actually accomplished anything that we could have done using ToList()

Where Clause

This is where this technique actually becomes more useful. Say we want a collection of all characters in a string that are lower case we could write a loop but with using a LINQ query we don’t have to think about all the details of actually checking if a character is lower case. We can then just do what processing we want to on those characters.

foreach (var c in s.Where(Char.IsLower))

The Char.IsLower predicate is called on each character and we skip any results that result false. So the following loop prints out all lower case letters:

string s = "WELCOME to codecall";
foreach (var c in s.Where(Char.IsLower))

The output will be:


Now, say you want all lower case characters and all numbers. You can write the Where clause as follows:
foreach (var c in s.Where(c => Char.IsLower(c) || Char.IsWhiteSpace(c)))

This is the main use of LINQ with strings is simplifying complicated string processing. Since LINQ deals primarily with lists we cannot implement IndexOf functions with LINQ.
We can, however, implement sorting of strings. Say we want to sort a string in increasing order of ascii value. We simply write:

var s2 = (from c in s
                 orderby c ascending
                 select c

What you get is all the characters in s sorted in ascending order but what you now have is a list not a string as the original result was. Using ToString() doesn`t seem to be giving nice results so we may have to iterate through the list to recreate a string.
When I do ToString() on the above query I get:

System.Linq.OrderdEnumerable `2[System.Char, System.Char]

To get around this restriction we either call ToList() or ToArray() to get something that we can work with. I’d like to leave this by saying that using Where to simplify filtering strings is useful and other things like sorting may be useful with certain restrictions. Not all string functions should use LINQ as in the length function they are really inefficient.
Any questions feel free to ask.
  • 0

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download