Friday, September 13, 2019

Reversing strings in c# using linq

A common requirement is reversing strings in c#.There are few different ways by which we can reverse strings.

Using the for loop for reversing strings

We iterate over the entire string and then just assign the characters to a different string.We loop through the original string in reverse order.

 Using the Reverse method of the array class

Reverse is a static method of the Array class.We can pass char array to this method and it will reverse the order of the characters.Once we get the characters in reverse order we can pass them to the string constructor to create the reversed string.

 Reversing strings in c# using LINQ

We can use the query operators in LINQ to reverse the strings.For reversing strings in c# using linq we use the ForEach() method which iterates over the entire collection.In this method we pass the logic to reverse the string.
By using the ForEach method we can avoid the for statement and directly loop over the string.

No comments:

Post a Comment

Get max value for identity column without a table scan

  You can use   IDENT_CURRENT   to look up the last identity value to be inserted, e.g. IDENT_CURRENT( 'MyTable' ) However, be caut...