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
)
public boolean IsPrefix (
	String source, 
	String prefix
)
public function IsPrefix (
	source : String, 
	prefix : String
) : boolean
Not applicable.

Parameters

source

The string to search in.

prefix

The string to compare with the beginning of source.

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

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

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

using System;
using System.Globalization;

public class SamplesCompareInfo  {

   public static void 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 "calle" and "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 "calle" and "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

*/

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

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

        // Determines whether myXfix is a suffix of "calle" and "llegar".
        Console.WriteLine("IsSuffix( {0}, {1} ) : {2}", myStr1, myXfix, 
            System.Convert.ToString(myComp.IsSuffix(myStr1, myXfix)));
        Console.WriteLine("IsSuffix( {0}, {1} ) : {2}", myStr2, myXfix, 
            System.Convert.ToString(myComp.IsSuffix(myStr2, myXfix)));
    } //main
} //SamplesCompareInfo

/*
This code produces the following output.

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

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: