Home
Search
VB.NET StartsWith and EndsWith String FunctionsUse StartsWith and EndsWith: call StartsWith to compare the first characters of 2 Strings.
StartsWith, EndsWith. In VB.NET StartsWith tests the first part of a String. It checks whether one String starts with the characters in another. EndsWith checks the last characters.
Some info. StartsWith is often useful. StartsWith supports the StringComparison.OrdinalIgnoreCase argument, which makes it case-insensitive. Without this argument, it is case-sensitive.
An example. Here we use StartsWith. This call is case-sensitive. We pass the StringComparison.OrdinalIgnoreCase enumerated constant. This makes "D" equal to "d" so StartsWith returns True.
Finally We show that StartsWith can also return False if the first String does not actually start with the second String.
Tip This logic is commonly needed in programs. We do not need to develop custom loops.
VB.NET program that uses StartsWith
Module Module1 Sub Main() Dim value As String = "Dot Net Perls" ' Use StartsWith. If value.StartsWith("Dot") Then Console.WriteLine(1) End If ' Use StartsWith ignoring case. If value.StartsWith("dot", StringComparison.OrdinalIgnoreCase) Then Console.WriteLine(2) End If ' It can return False. Console.WriteLine(value.StartsWith("Net")) End Sub End Module
1 2 False
Test chars. There is another way to check the first characters of a String. It is typically much faster. We test the Length of a String and then check characters by their indexes.
Tip The two If-statements shown here will always evaluate to the same truth value.
VB.NET program that checks first character
Module Module1 Sub Main() Dim value As String = "cat" ' You could use StartsWith... If value.StartsWith("c") Then Console.WriteLine(1) End If ' Or you could check the character yourself. If value.Length >= 1 And value(0) = "c" Then Console.WriteLine(2) End If End Sub End Module
1 2
EndsWith. This tests the end part of a String. With it we specify a String that is then tested against the end characters of the first String.
And If they match it returns true. This Function is commonly useful in VB.NET programs that process text.
Here Let's get started with this VB.NET program. We use a String containing the address of this web site.
Loop We use the For-Each loop construct to loop through all the possible extensions and then use EndsWith on the first String.
For Each, For
VB.NET program that uses EndsWith function
Module Module1 Sub Main() Dim value As String = "http://www.dotnetperls.com" Dim array() As String = {".net", ".org", ".edu", ".com"} For Each element As String In array If value.EndsWith(element) Then Console.WriteLine(element) End If Next End Sub End Module
.com
StringComparison. You can pass a StringComparison enum as the second argument. The two most useful options are probably StringComparison.Ordinal and StringComparison.OrdinalIgnoreCase.
Ordinal This means letters are treated exactly as they are stored (as numbers).
OrdinalIgnoreCase This means uppercase and lowercase letters are considered equal (meaning "A" equals "a").
A summary. We looked at the StartsWith and EndsWith methods. EndsWith is excellent for testing to see whether a String happens to start (or end) with another String.
Home
© 2007-2021 sam allen. see site info on the changelog