Home
Map
String StartsWith and EndsWith FunctionsUse StartsWith and EndsWith: call StartsWith to compare the first characters of 2 Strings.
VB.NET
This page was last reviewed on Sep 25, 2021.
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.
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.
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.
Detail We use the For-Each loop construct to loop through all the possible extensions and then use EndsWith on the first String.
For
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.
Detail This means letters are treated exactly as they are stored (as numbers).
Detail 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.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Sep 25, 2021 (edit).
Home
Changes
© 2007-2024 Sam Allen.