Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

CompareInfo.IsPrefix Method (String, String, CompareOptions)

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

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

'Declaration
Public Overridable Function IsPrefix ( _
	source As String, _
	prefix As String, _
	options As CompareOptions _
) As Boolean
'Usage
Dim instance As CompareInfo
Dim source As String
Dim prefix As String
Dim options As CompareOptions
Dim returnValue As Boolean

returnValue = instance.IsPrefix(source, prefix, options)
public boolean IsPrefix (
	String source, 
	String prefix, 
	CompareOptions options
)
public function IsPrefix (
	source : String, 
	prefix : String, 
	options : CompareOptions
) : boolean
Not applicable.

Parameters

source

The string to search in.

prefix

The string to compare with the beginning of source.

options

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

true if the length of prefix is less than or equal to the length of source and source starts with prefix; otherwise, false.
Exception typeCondition

ArgumentNullException

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

-or-

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

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 code example determines whether a string is the prefix or suffix of another string using CompareOptions.

Imports System
Imports System.Globalization

Public Class SamplesCompareInfo

   Public Shared Sub Main()

      ' 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

      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))

   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


import System.* ;
import System.Globalization.* ;

public class SamplesCompareInfo
{
    public static void main(String[] args)
    {
        // Defines the strings to compare.
        String myStr1 = "calle";
        String myStr2 = "llegar";
        String myXfix = "LLE";

        // Uses the CompareInfo property of the InvariantCulture.
        CompareInfo myComp = 
            CultureInfo.get_InvariantCulture().get_CompareInfo();

        Console.WriteLine("IsSuffix \"{0}\", \"{1}\"", myStr1, myXfix);
        Console.WriteLine("   With no CompareOptions            : {0}",
            System.Convert.ToString( myComp.IsSuffix(myStr1, myXfix)));
        Console.WriteLine("   With None                         : {0}",
            System.Convert.ToString(myComp.IsSuffix(myStr1, myXfix, 
            CompareOptions.None)));
        Console.WriteLine("   With Ordinal                      : {0}", 
            System.Convert.ToString(myComp.IsSuffix(myStr1, myXfix, 
            CompareOptions.Ordinal)));
        Console.WriteLine("   With IgnoreCase                   : {0}",
            System.Convert.ToString( myComp.IsSuffix(myStr1, myXfix, 
            CompareOptions.IgnoreCase)));

        Console.WriteLine("IsPrefix \"{0}\", \"{1}\"", myStr2, myXfix);
        Console.WriteLine("   With no CompareOptions            : {0}", 
            System.Convert.ToString(myComp.IsPrefix(myStr2, myXfix)));
        Console.WriteLine("   With None                         : {0}", 
            System.Convert.ToString(myComp.IsPrefix(myStr2, myXfix, 
            CompareOptions.None)));
        Console.WriteLine("   With Ordinal                      : {0}", 
            System.Convert.ToString(myComp.IsPrefix(myStr2, myXfix, 
            CompareOptions.Ordinal)));
        Console.WriteLine("   With IgnoreCase                   : {0}",
            System.Convert.ToString(myComp.IsPrefix(myStr2, myXfix, 
            CompareOptions.IgnoreCase)));
    } //main 
} //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
*/

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.