Windows apps
Collapse the table of content
Expand the table of content
Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

CompareInfo::IsPrefix Method (String^, String^)

 

Determines whether the specified source string starts with the specified prefix.

Namespace:   System.Globalization
Assembly:  mscorlib (in mscorlib.dll)

public:
virtual bool IsPrefix(
	String^ source,
	String^ prefix
)

Parameters

source
Type: System::String^

The string to search in.

prefix
Type: System::String^

The string to compare with the beginning of source.

Return Value

Type: System::Boolean

true 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 null.

-or-

prefix is null.

Every string starts and ends with an empty substring (""); therefore, if prefix is an empty string, this method returns true.

System_CAPS_noteNote

When possible, you should call string comparison methods that have a parameter of type CompareOptions to specify the kind of comparison expected. As a general rule, use linguistic options (using the current culture) for comparing strings displayed in the user interface and specify CompareOptions::Ordinal or CompareOptions::OrdinalIgnoreCase for security comparisons.

The following example determines whether a string is the prefix or suffix of another string.

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;

   // Determines whether myXfix is a prefix of S"calle" and S"llegar".
   Console::WriteLine( "IsPrefix( {0}, {1}) : {2}", myStr1, myXfix, myComp->IsPrefix( myStr1, myXfix ) );
   Console::WriteLine( "IsPrefix( {0}, {1}) : {2}", myStr2, myXfix, myComp->IsPrefix( myStr2, myXfix ) );

   // Determines whether myXfix is a suffix of S"calle" and S"llegar".
   Console::WriteLine( "IsSuffix( {0}, {1}) : {2}", myStr1, myXfix, myComp->IsSuffix( myStr1, myXfix ) );
   Console::WriteLine( "IsSuffix( {0}, {1}) : {2}", myStr2, myXfix, myComp->IsSuffix( myStr2, myXfix ) );
}

/*
This code produces the following output.

IsPrefix(calle, lle) : False
IsPrefix(llegar, lle) : True
IsSuffix(calle, lle) : True
IsSuffix(llegar, lle) : False

*/

Universal Windows Platform
Available since 8
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Return to top
Show:
© 2017 Microsoft