Some Common Operations using List<string> - Part II
In Part I of this article, I shared some common operations on List<string>. You can read the article over here Some Common Operations using List - Part I. In this article, I will share some more examples that demonstrate common operations on List<string>
Here are the sample List<string> declarations for demonstration purposes. The VB.NET code has been converted using a conversion tool and has not been tested.
C#
List<string> lstOne = new List<string>() { "January", "February", "March"};
List<string> lstTwo = new List<string>() { "January", "April", "March"};
List<string> lstThree = new List<string>() { "January", "April", "March", "May" };
List<string> lstFour = new List<string>() { "Jan", "Feb", "Jan", "April", "Feb" };
IEnumerable<string> lstNew = null;
VB.NET
Dim lstOne As New List(Of String) (New String() {"January", "February", "March"})
Dim lstTwo As New List(Of String) (New String() {"January", "April", "March"})
Dim lstThree As New List(Of String) (New String() {"January", "April", "March", "May"})
Dim lstFour As New List(Of String) (New String() {"Jan", "Feb", "Jan", "April", "Feb"})
Dim lstNew As IEnumerable(Of String) = Nothing
We will be printing the results on the console using a simple method shown here:
C#
static void PrintList(IEnumerable<string> str)
{
foreach (var s in str)
Console.WriteLine(s);
Console.WriteLine("-------------");
}
VB.NET
Shared Sub PrintList(ByVal str As IEnumerable(Of String))
For Each s In str
Console.WriteLine(s)
Next s
Console.WriteLine("-------------")
End Sub
Let us get started.
1. Order the elements of a List<string> first by Length and then by words
C#
// Order by Length then by words (descending)
lstNew = lstThree.OrderBy(x => x.Length)
.ThenByDescending(x => x);
PrintList(lstNew);
VB.NET
lstNew = lstThree.OrderBy(Function(x) x.Length).ThenByDescending(Function(x) x)
PrintList(lstNew)
OUTPUT
2. Create a new string by combining all words of a List<string>
C#
// Create a string by combining all words of a List<string>
// Use StringBuilder if you want performance
string delim = ",";
var str = lstOne.Aggregate((x, y) => x + delim + y);
Console.WriteLine(str);
Console.WriteLine("-------------");
VB.NET
Dim delim As String = ","
Dim str = lstOne.Aggregate(Function(x, y) x & delim & y)
Console.WriteLine(str)
Console.WriteLine("-------------")
Note: You should use a StringBuilder for performance
OUTPUT
3. Create a List<string> from a Delimited string
C#
// Create a List<string> from a Delimited string
string s = "January February March";
char separator = ' ';
lstNew = s.Split(separator).ToList();
PrintList(lstNew);
VB.NET
Dim s As String = "January February March"
Dim separator As Char = " "c
lstNew = s.Split(separator).ToList()
PrintList(lstNew)
OUTPUT
4. Convert a List<int> to List<string>
C#
// Convert a List<int> to List<string>
List<int> lstNum = new List<int>(new int[] { 3, 6, 7, 9 });
lstNew = lstNum.ConvertAll<string>(delegate(int i)
{
return i.ToString();
});
PrintList(lstNew);
VB.NET
Dim lstNum As New List(Of Integer)(New Integer() { 3, 6, 7, 9 })
lstNew = lstNum.ConvertAll(Of String)(Function(i As Integer) i.ToString())
PrintList(lstNew)
OUTPUT
5. Count Repeated Words in a List<string>
C#
// Count Repeated Words
var q = lstFour.GroupBy(x => x)
.Select(g => new { Value = g.Key, Count = g.Count() })
.OrderByDescending(x => x.Count);
foreach (var x in q)
{
Console.WriteLine("Value: " + x.Value + " Count: " + x.Count);
}
VB.NET
Dim q = lstFour.GroupBy(Function(x) x).Select(Function(g) New With {Key .Value = g.Key, Key .Count = g.Count()}).OrderByDescending(Function(x) x.Count)
For Each x In q
Console.WriteLine("Value: " & x.Value & " Count: " & x.Count)
Next x
OUTPUT
I hope you liked this article and I thank you for viewing it.
Give me a +1 if you think it was a good article. Thanks!