Determines whether the beginning of this string instance matches the specified string when compared using the specified comparison option.
public:
bool StartsWith(System::String ^ value, StringComparison comparisonType);
public bool StartsWith(string value, StringComparison comparisonType);
[System.Runtime.InteropServices.ComVisible(false)]
public bool StartsWith(string value, StringComparison comparisonType);
member this.StartsWith : string * StringComparison -> bool
[<System.Runtime.InteropServices.ComVisible(false)>]
member this.StartsWith : string * StringComparison -> bool
Public Function StartsWith (value As String, comparisonType As StringComparison) As Boolean
Parameters
The string to compare.
One of the enumeration values that determines how this string and value
are compared.
true
if this instance begins with value
; otherwise, false
.
The following example searches for the string "the" at the beginning of a longer string that begins with the word "The". As the output from the example shows, a call to the StartsWith(String, StringComparison) method that performs a culture-insensitive but case-sensitive comparison fails to match the string, while a call that performs a culture- and case-insensitive comparison matches the string.
using System;
public class Example
{
public static void Main()
{
String title = "The House of the Seven Gables";
String searchString = "the";
StringComparison comparison = StringComparison.InvariantCulture;
Console.WriteLine("'{0}':", title);
Console.WriteLine(" Starts with '{0}' ({1:G} comparison): {2}",
searchString, comparison,
title.StartsWith(searchString, comparison));
comparison = StringComparison.InvariantCultureIgnoreCase;
Console.WriteLine(" Starts with '{0}' ({1:G} comparison): {2}",
searchString, comparison,
title.StartsWith(searchString, comparison));
}
}
// The example displays the following output:
// 'The House of the Seven Gables':
// Starts with 'the' (InvariantCulture comparison): False
// Starts with 'the' (InvariantCultureIgnoreCase comparison): True
open System
let title = "The House of the Seven Gables"
let searchString = "the"
let comparison = StringComparison.InvariantCulture
printfn $"'{title}':"
printfn $" Starts with '{searchString}' ({comparison:G} comparison): {title.StartsWith(searchString, comparison)}"
let comparison2 = StringComparison.InvariantCultureIgnoreCase
printfn $" Starts with '{searchString}' ({comparison2:G} comparison): {title.StartsWith(searchString, comparison2)}"
// The example displays the following output:
// 'The House of the Seven Gables':
// Starts with 'the' (InvariantCulture comparison): False
// Starts with 'the' (InvariantCultureIgnoreCase comparison): True
Module Example
Public Sub Main()
Dim title As String = "The House of the Seven Gables"
Dim searchString As String = "the"
Dim comparison As StringComparison = StringComparison.InvariantCulture
Console.WriteLine("'{0}':", title)
Console.WriteLine(" Starts with '{0}' ({1:G} comparison): {2}",
searchString, comparison,
title.StartsWith(searchString, comparison))
comparison = StringComparison.InvariantCultureIgnoreCase
Console.WriteLine(" Starts with '{0}' ({1:G} comparison): {2}",
searchString, comparison,
title.StartsWith(searchString, comparison))
End Sub
End Module
' The example displays the following output:
' 'The House of the Seven Gables':
' Starts with 'the' (InvariantCulture comparison): False
' Starts with 'the' (InvariantCultureIgnoreCase comparison): True
The following example determines whether a string starts with a particular substring. It initializes a two-dimensional string array. The first element in the second dimension contains a string, and the second element contains the string to search for at the start of the first string. The results are affected by the choice of culture, whether case is ignored, and whether an ordinal comparison is performed. Note that when the string instance contains a ligature, culture-sensitive comparisons with its consecutive characters successfully match.
using System;
public class Example
{
public static void Main()
{
string[,] strings = { {"ABCdef", "abc" },
{"ABCdef", "abc" },
{"Åil","oe" },
{ "læring}", "lae" } };
for (int ctr1 = strings.GetLowerBound(0); ctr1 <= strings.GetUpperBound(0); ctr1++)
{
foreach (string cmpName in Enum.GetNames(typeof(StringComparison)))
{
StringComparison strCmp = (StringComparison) Enum.Parse(typeof(StringComparison),
cmpName);
string instance = strings[ctr1, 0];
string value = strings[ctr1, 1];
Console.WriteLine("{0} starts with {1}: {2} ({3} comparison)",
instance, value,
instance.StartsWith(value, strCmp),
strCmp);
}
Console.WriteLine();
}
}
}
// The example displays the following output:
// ABCdef starts with abc: False (CurrentCulture comparison)
// ABCdef starts with abc: True (CurrentCultureIgnoreCase comparison)
// ABCdef starts with abc: False (InvariantCulture comparison)
// ABCdef starts with abc: True (InvariantCultureIgnoreCase comparison)
// ABCdef starts with abc: False (Ordinal comparison)
// ABCdef starts with abc: True (OrdinalIgnoreCase comparison)
//
// ABCdef starts with abc: False (CurrentCulture comparison)
// ABCdef starts with abc: True (CurrentCultureIgnoreCase comparison)
// ABCdef starts with abc: False (InvariantCulture comparison)
// ABCdef starts with abc: True (InvariantCultureIgnoreCase comparison)
// ABCdef starts with abc: False (Ordinal comparison)
// ABCdef starts with abc: True (OrdinalIgnoreCase comparison)
//
// Åil starts with oe: True (CurrentCulture comparison)
// Åil starts with oe: True (CurrentCultureIgnoreCase comparison)
// Åil starts with oe: True (InvariantCulture comparison)
// Åil starts with oe: True (InvariantCultureIgnoreCase comparison)
// Åil starts with oe: False (Ordinal comparison)
// Åil starts with oe: False (OrdinalIgnoreCase comparison)
//
// læring} starts with lae: True (CurrentCulture comparison)
// læring} starts with lae: True (CurrentCultureIgnoreCase comparison)
// læring} starts with lae: True (InvariantCulture comparison)
// læring} starts with lae: True (InvariantCultureIgnoreCase comparison)
// læring} starts with lae: False (Ordinal comparison)
// læring} starts with lae: False (OrdinalIgnoreCase comparison)
open System
let strings =
array2D
[ [ "ABCdef"; "abc" ]
[ "ABCdef"; "abc" ]
[ "Åil"; "oe" ]
[ "læring}"; "lae" ] ]
for ctr1 = strings.GetLowerBound 0 to strings.GetUpperBound 0 do
for cmpName in Enum.GetNames typeof<StringComparison> do
let strCmp = Enum.Parse(typeof<StringComparison>, cmpName) :?> StringComparison
let instance = strings[ctr1, 0]
let value = strings[ctr1, 1]
printfn $"{instance} starts with {value}: {instance.StartsWith(value, strCmp)} ({strCmp} comparison)"
printfn ""
// The example displays the following output:
// ABCdef starts with abc: False (CurrentCulture comparison)
// ABCdef starts with abc: True (CurrentCultureIgnoreCase comparison)
// ABCdef starts with abc: False (InvariantCulture comparison)
// ABCdef starts with abc: True (InvariantCultureIgnoreCase comparison)
// ABCdef starts with abc: False (Ordinal comparison)
// ABCdef starts with abc: True (OrdinalIgnoreCase comparison)
//
// ABCdef starts with abc: False (CurrentCulture comparison)
// ABCdef starts with abc: True (CurrentCultureIgnoreCase comparison)
// ABCdef starts with abc: False (InvariantCulture comparison)
// ABCdef starts with abc: True (InvariantCultureIgnoreCase comparison)
// ABCdef starts with abc: False (Ordinal comparison)
// ABCdef starts with abc: True (OrdinalIgnoreCase comparison)
//
// Åil starts with oe: True (CurrentCulture comparison)
// Åil starts with oe: True (CurrentCultureIgnoreCase comparison)
// Åil starts with oe: True (InvariantCulture comparison)
// Åil starts with oe: True (InvariantCultureIgnoreCase comparison)
// Åil starts with oe: False (Ordinal comparison)
// Åil starts with oe: False (OrdinalIgnoreCase comparison)
//
// læring} starts with lae: True (CurrentCulture comparison)
// læring} starts with lae: True (CurrentCultureIgnoreCase comparison)
// læring} starts with lae: True (InvariantCulture comparison)
// læring} starts with lae: True (InvariantCultureIgnoreCase comparison)
// læring} starts with lae: False (Ordinal comparison)
// læring} starts with lae: False (OrdinalIgnoreCase comparison)
Module Example
Public Sub Main()
Dim strings(,) As String = { {"ABCdef", "abc" },
{"ABCdef", "abc" },
{"Åil","oe" },
{ "læring}", "lae" } }
For ctr1 As Integer = strings.GetLowerBound(0) To strings.GetUpperBound(0)
For Each cmpName As String In [Enum].GetNames(GetType(StringComparison))
Dim strCmp As StringComparison = CType([Enum].Parse(GetType(StringComparison),
cmpName), StringComparison)
Dim instance As String = strings(ctr1, 0)
Dim value As String = strings(ctr1, 1)
Console.WriteLine("{0} starts with {1}: {2} ({3} comparison)",
instance, value,
instance.StartsWith(value, strCmp),
strCmp)
Next
Console.WriteLine()
Next
End Sub
End Module
' The example displays the following output:
' ABCdef starts with abc: False (CurrentCulture comparison)
' ABCdef starts with abc: True (CurrentCultureIgnoreCase comparison)
' ABCdef starts with abc: False (InvariantCulture comparison)
' ABCdef starts with abc: True (InvariantCultureIgnoreCase comparison)
' ABCdef starts with abc: False (Ordinal comparison)
' ABCdef starts with abc: True (OrdinalIgnoreCase comparison)
'
' ABCdef starts with abc: False (CurrentCulture comparison)
' ABCdef starts with abc: True (CurrentCultureIgnoreCase comparison)
' ABCdef starts with abc: False (InvariantCulture comparison)
' ABCdef starts with abc: True (InvariantCultureIgnoreCase comparison)
' ABCdef starts with abc: False (Ordinal comparison)
' ABCdef starts with abc: True (OrdinalIgnoreCase comparison)
'
' Åil starts with oe: True (CurrentCulture comparison)
' Åil starts with oe: True (CurrentCultureIgnoreCase comparison)
' Åil starts with oe: True (InvariantCulture comparison)
' Åil starts with oe: True (InvariantCultureIgnoreCase comparison)
' Åil starts with oe: False (Ordinal comparison)
' Åil starts with oe: False (OrdinalIgnoreCase comparison)
'
' læring} starts with lae: True (CurrentCulture comparison)
' læring} starts with lae: True (CurrentCultureIgnoreCase comparison)
' læring} starts with lae: True (InvariantCulture comparison)
' læring} starts with lae: True (InvariantCultureIgnoreCase comparison)
' læring} starts with lae: False (Ordinal comparison)
' læring} starts with lae: False (OrdinalIgnoreCase comparison)
Remarks
The StartsWith method compares the value
parameter to the substring at the beginning of this string and returns a value that indicates whether they are equal. To be equal, value
must be a reference to this same string, must be the empty string (""), or must match the beginning of this string. The type of comparison performed by the StartsWith method depends on the value of the comparisonType
parameter. The comparison can use the conventions of the current culture (StringComparison.CurrentCulture and StringComparison.CurrentCultureIgnoreCase) or the invariant culture (StringComparison.InvariantCulture and StringComparison.InvariantCultureIgnoreCase), or it can consist of a character-by-character comparison of code points (StringComparison.Ordinal or StringComparison.OrdinalIgnoreCase). The comparison can also be case-sensitive (StringComparison.CurrentCulture, StringComparison.InvariantCulture, or StringComparison.Ordinal), or it can ignore case (StringComparison.CurrentCultureIgnoreCase, StringComparison.InvariantCultureIgnoreCase, StringComparison.OrdinalIgnoreCase).
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4