CompareInfo::IsPrefix Method (String, String, CompareOptions)
Determines whether the specified source string starts with the specified prefix using the specified CompareOptions value.
Assembly: mscorlib (in mscorlib.dll)
Parameters
- source
- Type: System::String
The string to search in.
- prefix
- Type: System::String
The string to compare with the beginning of source.
- options
- Type: System.Globalization::CompareOptions
The CompareOptions value that defines how source and prefix should be compared. options is either the value Ordinal used by itself, or the bitwise combination of one or more of the following values: IgnoreCase, IgnoreSymbols, IgnoreNonSpace, IgnoreWidth, and IgnoreKanaType.
Return Value
Type: System::Booleantrue if the length of prefix is less than or equal to the length of source and source starts with prefix; otherwise, false.
| Exception | Condition |
|---|---|
| ArgumentNullException | source is nullptr. -or- prefix is nullptr. |
| ArgumentException | options contains an invalid CompareOptions value. |
Every string starts and ends with an empty substring (""); therefore, if prefix is an empty string, this method returns true.
The CompareOptions::StringSort value is not valid for this method.
Note |
|---|
When possible, the application should use string comparison methods that accept a CompareOptions value to specify the kind of comparison expected. As a general rule, user-facing comparisons are best served by the use of linguistic options (using the current culture), while security comparisons should specify Ordinal or OrdinalIgnoreCase. |
The following code example determines whether a string is the prefix or suffix of another string using CompareOptions.
using namespace System; using namespace System::Globalization; int main() { // Defines the strings to compare. String^ myStr1 = "calle"; String^ myStr2 = "llegar"; String^ myXfix = "LLE"; // Uses the CompareInfo property of the InvariantCulture. CompareInfo^ myComp = CultureInfo::InvariantCulture->CompareInfo; Console::WriteLine( "IsSuffix \"{0}\", \"{1}\"", myStr1, myXfix ); Console::WriteLine( " With no CompareOptions : {0}", myComp->IsSuffix( myStr1, myXfix ) ); Console::WriteLine( " With None : {0}", myComp->IsSuffix( myStr1, myXfix, CompareOptions::None ) ); Console::WriteLine( " With Ordinal : {0}", myComp->IsSuffix( myStr1, myXfix, CompareOptions::Ordinal ) ); Console::WriteLine( " With IgnoreCase : {0}", myComp->IsSuffix( myStr1, myXfix, CompareOptions::IgnoreCase ) ); Console::WriteLine( "IsPrefix \"{0}\", \"{1}\"", myStr2, myXfix ); Console::WriteLine( " With no CompareOptions : {0}", myComp->IsPrefix( myStr2, myXfix ) ); Console::WriteLine( " With None : {0}", myComp->IsPrefix( myStr2, myXfix, CompareOptions::None ) ); Console::WriteLine( " With Ordinal : {0}", myComp->IsPrefix( myStr2, myXfix, CompareOptions::Ordinal ) ); Console::WriteLine( " With IgnoreCase : {0}", myComp->IsPrefix( myStr2, myXfix, CompareOptions::IgnoreCase ) ); } /* This code produces the following output. IsSuffix "calle", "LLE" With no CompareOptions : False With None : False With Ordinal : False With IgnoreCase : True IsPrefix "llegar", "LLE" With no CompareOptions : False With None : False With Ordinal : False With IgnoreCase : True */
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Note