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:
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:
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
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:
To negate this operation, apply the Not operator to it.
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:
Because the String.Contains() method is Boolean, to negate its result, you can precede it with the Not operator. |
|
|||||||
|