CompareInfo.IsPrefix Method (String, String, CompareOptions)

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Determines whether the specified source string starts with the specified prefix using the specified CompareOptions value.

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

Syntax

'Declaration
<SecuritySafeCriticalAttribute> _
Public Overridable Function IsPrefix ( _
    source As String, _
    prefix As String, _
    options As CompareOptions _
) As Boolean
[SecuritySafeCriticalAttribute]
public virtual bool IsPrefix(
    string source,
    string prefix,
    CompareOptions options
)

Parameters

  • 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.

Exceptions

Exception Condition
ArgumentNullException

source is nulla null reference (Nothing in Visual Basic).

-or-

prefix is nulla null reference (Nothing in Visual Basic).

ArgumentException

options contains an invalid CompareOptions value.

Remarks

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.

Examples

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

Imports System.Globalization

Public Class Example

   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)

      ' Defines the strings to compare.
      Dim myStr1 As [String] = "calle"
      Dim myStr2 As [String] = "llegar"
      Dim myXfix As [String] = "LLE"

      ' Uses the CompareInfo property of the InvariantCulture.
      Dim myComp As CompareInfo = CultureInfo.InvariantCulture.CompareInfo

      outputBlock.Text += String.Format("IsSuffix ""{0}"", ""{1}""", myStr1, myXfix) & vbCrLf
      outputBlock.Text += String.Format("   With no CompareOptions            : {0}", myComp.IsSuffix(myStr1, myXfix)) & vbCrLf
      outputBlock.Text += String.Format("   With None                         : {0}", myComp.IsSuffix(myStr1, myXfix, CompareOptions.None)) & vbCrLf
      outputBlock.Text += String.Format("   With Ordinal                      : {0}", myComp.IsSuffix(myStr1, myXfix, CompareOptions.Ordinal)) & vbCrLf
      outputBlock.Text += String.Format("   With IgnoreCase                   : {0}", myComp.IsSuffix(myStr1, myXfix, CompareOptions.IgnoreCase)) & vbCrLf

      outputBlock.Text += String.Format("IsPrefix ""{0}"", ""{1}""", myStr2, myXfix) & vbCrLf
      outputBlock.Text += String.Format("   With no CompareOptions            : {0}", myComp.IsPrefix(myStr2, myXfix)) & vbCrLf
      outputBlock.Text += String.Format("   With None                         : {0}", myComp.IsPrefix(myStr2, myXfix, CompareOptions.None)) & vbCrLf
      outputBlock.Text += String.Format("   With Ordinal                      : {0}", myComp.IsPrefix(myStr2, myXfix, CompareOptions.Ordinal)) & vbCrLf
      outputBlock.Text += String.Format("   With IgnoreCase                   : {0}", myComp.IsPrefix(myStr2, myXfix, CompareOptions.IgnoreCase)) & vbCrLf

   End Sub 'Main 

End Class 'SamplesCompareInfo


'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

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

*/

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.