Home

LINQ and the String Class

     

Introduction

 

In case you have not paid attention to it in the past, the String starts as follows:

Public NotInheritable Class String _
    Implements IComparable, ICloneable, IConvertible, IComparable(Of String),  _
    IEnumerable(Of Char), IEnumerable, IEquatable(Of String)
   

As you can see, the String class implements:

  • The IEnumerable interface. This means that it is already ready for all possible operations of the LINQ
  • The IEnumerable(Of Char) interface. This results in a string being a collection of characters. Consequently, all normal collection operations can be performed on a string and each character or symbol is treated as an item
  • The IComparable and the IComparable(Of String) interfaces. This gives the ability to compare two strings

Based on this, the String class holds a special position in LINQ. If you create a LINQ statement whose select expression produces a string value, the String class makes available its properties and methods so you can use them to refine your Where statement.

Concatenating the Results of a Select Statement

We saw how you can use one value that is a member of an object of a class as produced by a select statement:

Dim LastNames = From N
                In Empls 
                Select N.LastName;

To add other values to the select statement, that is, to access more that one member of the class, you can concatenate them. The String class provides various solutions.

The String class is equipped with the Concat() method that is overloaded with 10 versions. You can use any of those versions to add strings. Here is an example:

Imports System.Linq
Imports System.Collections.Generic

Public Class Employee
    Public EmployeeNumber As Integer
    Public FirstName As String
    Public LastName As String
    Public HourlySalary As Double

    Public Sub New(Optional ByVal Number As Integer = 0,
                   Optional ByVal FName As String = "John",
                   Optional ByVal LName As String = "Doe",
                   Optional ByVal salary As Double = 0D)
        EmployeeNumber = Number
        FirstName = FName
        LastName = LName
        HourlySalary = salary
    End Sub
End Class

Module Exercise
    Public Function Main() As Integer
        Dim Employees() As Employee = 
        {
            New Employee(971974, "Patricia", "Katts", 24.68),
            New Employee(208411, "Raymond", "Kouma", 20.15),
            New Employee(279374, "Hélène", "Mukoko", 15.55),
            New Employee(707912, "Bertrand", "Yamaguchi", 24.68),
            New Employee(971394, "Gertrude", "Monay", 20.55)
        }

        Dim Empls = From StaffMembers
                    In Employees
                    Select String.Concat(CStr(StaffMembers.EmployeeNumber), ", ",
                                         StaffMembers.FirstName, " ",
                                         StaffMembers.LastName, ", ",
                                         CStr(StaffMembers.HourlySalary))

        For Each Empl In Empls
            Console.WriteLine(Empl)
        Next

        Console.WriteLine()
        Return 0
    End Function
End Module

This would produce:

Employees

Alternatively, you can use the + operator that is overloaded in the string class. Here is an example:

Module Exercise
    Public Function Main() As Integer
        Dim Employees() As Employee = {
            New Employee(971974, "Patricia", "Katts", 24.68),
            New Employee(208411, "Raymond", "Kouma", 20.15),
            New Employee(279374, "Hélène", "Mukoko", 15.55),
            New Employee(707912, "Bertrand", "Yamaguchi", 24.68),
            New Employee(294800, "Peter", "Mukoko", 18.85),
            New Employee(971394, "Gertrude", "Monay", 20.55)
        }

        Dim Empls = From StaffMembers
                    In Employees
                    Select StaffMembers.LastName + ", " + StaffMembers.FirstName

        For Each Empl In Empls
            Console.WriteLine(Empl)
        Next

        Console.WriteLine()
        Return 0
    End Function
End Module

This would produce:

Employees

Two other solutions consist of using either the Format() of the Join() methods (of the String class).

Checking for String Equality

Like all classes of the .NET Framework, the String class inherits the Equals() method of the Object class. This makes it possible to compare two strings for equality. Besides the Equals() method, the = operator is overloaded in the String class and has the same effect. Here is an example of using it:

Imports System.Linq
Imports System.Collections.Generic

Module Exercise
    Public Function Main() As Integer
        Dim Names() =
     	{
            "Hermine", "Patrick", "Hank", "Bertine",
            "Julius", "Thomas", "Jeannette", "Patrick",
            "Patrick", "Raul", "David", "Paulette"
     	}

        Dim Name = From N
                   In Names
                   Where N = "Patrick"
                   Select N

        For Each member In Name
            Console.WriteLine(member)
        Next

        Console.WriteLine()
        Return 0
    End Function
End Module

This would produce:

Employees

If you are working on a class, you can apply the equality operator on one of its properties. Here is an example:

Imports System.Linq
Imports System.Collections.Generic

Public Class Employee
    Public EmployeeNumber As Integer
    Public FirstName As String
    Public LastName As String
    Public HourlySalary As Double

    Public Sub New(Optional ByVal Number As Integer = 0,
                   Optional ByVal FName As String = "John",
                   Optional ByVal LName As String = "Doe",
                   Optional ByVal salary As Double = 0D)
        EmployeeNumber = Number
        FirstName = FName
        LastName = LName
        HourlySalary = salary
    End Sub
End Class

Module Exercise
    Public Function Main() As Integer
        Dim Employees() As Employee = {
            New Employee(971974, "Patricia", "Katts", 24.68),
            New Employee(208411, "Raymond", "Kouma", 20.15),
            New Employee(279374, "Hélène", "Mukoko", 15.55),
            New Employee(707912, "Bertrand", "Yamaguchi", 24.68),
            New Employee(294800, "Peter", "Mukoko", 18.85),
            New Employee(971394, "Gertrude", "Monay", 20.55)
        }

        Dim FullNames = From Empls
                        In Employees
                        Where Empls.LastName = "Mukoko"
                        Select Empls.LastName + ", " + Empls.FirstName

        For Each Empl In FullNames
            Console.WriteLine(Empl)
        Next

        Console.WriteLine()
        Return 0
    End Function
End Module

This would produce:

Employees

On the other hand, to find out if two strings are not equal, you can use the <> operator.

 
 
 

Checking the Starting Sub-String

The String class is equipped with various types of methods to work on sub-strings. For example, to get a list of strings that start with a certain letter or a sub-string, you can call the StartsWith() method. This is a Boolean method that is overloaded with three versions. One of the versions takes one argument that is the sub-string to find at the beginning of the string that calls it. Here is an example:

Imports System.Linq
Imports System.Collections.Generic

Module Exercise
    Public Function Main() As Integer
        Dim Name = 
     	{
            "Hermine", "Patrick", "Hank", "Bertine",
            "Julius", "Thomas", "Jeannette", "Patricia",
            "Henriette", "Raul", "David", "Paulette"
     	}

        Dim Names = From N
                    In Name
                    Where N.StartsWith("P")
                    Select N

        For Each Member In Names
            Console.WriteLine(Member)
        Next

        Console.WriteLine()
        Return 0
    End Function
End Module

This would produce:

Starts With

The String.StartsWith() method returns True if the variable that called it starts with the argument. You can negate the result to find the strings that don't start with the argument. To do this, apply the Not operator. Here is an example:

Imports System.Linq
Imports System.Collections.Generic

Module Exercise
    Public Function Main() As Integer
        Dim Name = 
     	{
            "Hermine", "Patrick", "Hank", "Bertine",
            "Julius", "Thomas", "Jeannette", "Patricia",
            "Henriette", "Raul", "David", "Paulette"
     	}

        Dim Names = From N
                    In Name
                    Where Not N.StartsWith("P")
                    Select N

        For Each Member In Names
            Console.WriteLine(Member)
        Next

        Console.WriteLine()
        Return 0
    End Function
End Module

This would produce:

Starts With

To make the expression easy to read, you should include it in parentheses. Here is an example:

Imports System.Linq
Imports System.Collections.Generic

Module Exercise
    Public Function Main() As Integer
        Dim Name = 
     	{
            "Hermine", "Patrick", "Hank", "Bertine",
            "Julius", "Thomas", "Jeannette", "Patricia",
            "Henriette", "Raul", "David", "Paulette"
     	}

        Dim Names = From N
                    In Name
                    Where Not (N.StartsWith("P"))
                    Select N

        For Each Member In Names
            Console.WriteLine(Member)
        Next

        Console.WriteLine()
        Return 0
    End Function
End Module

Checking the Ending Sub-String

To get a list of strings that end with a certain letter or sub-string, you would call the String.EndsWith() method. Here is an example:

Imports System.Linq
Imports System.Collections.Generic

Module Exercise
    Public Function Main() As Integer
        Dim Name = 
     	{
            "Hermine", "Patrick", "Hank", "Bertine",
            "Julius", "Thomas", "Jeannette", "Patricia",
            "Henriette", "Raul", "David", "Paulette"
     	}

        Dim Names = From N
                    In Name
                    Where N.EndsWith("ette")
                    Select N

        For Each Member In Names
            Console.WriteLine(Member)
        Next

        Console.WriteLine()
        Return 0
    End Function
End Module

This would produce:

Ends With

To negate this operation, apply the Not operator to it.

Checking the Existence of a Sub-String

In the same way, to get a Select list of strings that contain a certain symbol or sub-string, you can call the String.Contains() method as follows:

Imports System.Linq
Imports System.Collections.Generic

Module Exercise
    Public Function Main() As Integer
        Dim Name =
        {
            "Hermine", "Patrick", "Hank", "Bertine",
            "Julius", "Thomas", "Jeannette", "Patricia",
            "Henriette", "Raul", "David", "Paulette"
        }

        Dim Names = From N
                    In Name
                    Where N.Contains("au")
                    Select N

        For Each Member In Names
            Console.WriteLine(Member)
        Next

        Console.WriteLine()
        Return 0
    End Function
End Module

This would produce:

Contains

Because the String.Contains() method is Boolean, to negate its result, you can precede it with the Not operator.

 
 
   
 

Previous Copyright © 2010-2016, FunctionX Next