CompareInfo.IsPrefix Method (String, String, CompareOptions)
[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]
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 null. -or- prefix is null. |
| 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.
The following example determines whether a string is the prefix or suffix of another string using CompareOptions.
using System; using System.Globalization; public class Example { public static void Demo(System.Windows.Controls.TextBlock outputBlock) { // 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; outputBlock.Text += String.Format("IsSuffix \"{0}\", \"{1}\"", myStr1, myXfix) + "\n"; outputBlock.Text += String.Format(" With no CompareOptions : {0}", myComp.IsSuffix(myStr1, myXfix)) + "\n"; outputBlock.Text += String.Format(" With None : {0}", myComp.IsSuffix(myStr1, myXfix, CompareOptions.None)) + "\n"; outputBlock.Text += String.Format(" With Ordinal : {0}", myComp.IsSuffix(myStr1, myXfix, CompareOptions.Ordinal)) + "\n"; outputBlock.Text += String.Format(" With IgnoreCase : {0}", myComp.IsSuffix(myStr1, myXfix, CompareOptions.IgnoreCase)) + "\n"; outputBlock.Text += String.Format("IsPrefix \"{0}\", \"{1}\"", myStr2, myXfix) + "\n"; outputBlock.Text += String.Format(" With no CompareOptions : {0}", myComp.IsPrefix(myStr2, myXfix)) + "\n"; outputBlock.Text += String.Format(" With None : {0}", myComp.IsPrefix(myStr2, myXfix, CompareOptions.None)) + "\n"; outputBlock.Text += String.Format(" With Ordinal : {0}", myComp.IsPrefix(myStr2, myXfix, CompareOptions.Ordinal)) + "\n"; outputBlock.Text += String.Format(" With IgnoreCase : {0}", myComp.IsPrefix(myStr2, myXfix, CompareOptions.IgnoreCase)) + "\n"; } } /* 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 */