CompareInfo.IsPrefix Method (String, String)
[ 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.
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.
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. |
The following example determines whether a string is the prefix or suffix of another string.
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; // Determines whether myXfix is a prefix of "calle" and "llegar". outputBlock.Text += String.Format("IsPrefix( {0}, {1} ) : {2}", myStr1, myXfix, myComp.IsPrefix(myStr1, myXfix)) + "\n"; outputBlock.Text += String.Format("IsPrefix( {0}, {1} ) : {2}", myStr2, myXfix, myComp.IsPrefix(myStr2, myXfix)) + "\n"; // Determines whether myXfix is a suffix of "calle" and "llegar". outputBlock.Text += String.Format("IsSuffix( {0}, {1} ) : {2}", myStr1, myXfix, myComp.IsSuffix(myStr1, myXfix)) + "\n"; outputBlock.Text += String.Format("IsSuffix( {0}, {1} ) : {2}", myStr2, myXfix, myComp.IsSuffix(myStr2, myXfix)) + "\n"; } } /* This code produces the following output. IsPrefix( calle, lle ) : False IsPrefix( llegar, lle ) : True IsSuffix( calle, lle ) : True IsSuffix( llegar, lle ) : False */
Show: