C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
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 = "The Dev Codes"
' 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
Output
1
2
False
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
Output
1
2
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, ForVB.NET program that uses EndsWith function
Module Module1
Sub Main()
Dim value As String = "http://www.dotnetCodex.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
Output
.com
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").