CompareInfo.IsPrefix Método

Definição

Determina se uma cadeia de caracteres começa com um prefixo específico.

Sobrecargas

Nome Description
IsPrefix(String, String)

Determina se a cadeia de caracteres de origem especificada começa com o prefixo especificado.

IsPrefix(String, String, CompareOptions)

Determina se a cadeia de caracteres de origem especificada começa com o prefixo especificado usando o valor especificado CompareOptions .

IsPrefix(String, String)

Determina se a cadeia de caracteres de origem especificada começa com o prefixo especificado.

public:
 virtual bool IsPrefix(System::String ^ source, System::String ^ prefix);
public virtual bool IsPrefix(string source, string prefix);
abstract member IsPrefix : string * string -> bool
override this.IsPrefix : string * string -> bool
Public Overridable Function IsPrefix (source As String, prefix As String) As Boolean

Parâmetros

source
String

A cadeia de caracteres na qual pesquisar.

prefix
String

A cadeia de caracteres a ser comparada com o início de source.

Retornos

true se o comprimento for prefix menor ou igual ao comprimento de source e source começar com prefix; caso contrário, false.

Exceções

source é null.

-ou-

prefix é null.

Exemplos

O exemplo a seguir determina se uma cadeia de caracteres é o prefixo ou sufixo de outra cadeia de caracteres.

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

*/
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

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

   End Sub

End Class


'This code produces the following output.
'
'IsPrefix( calle, lle ) : False
'IsPrefix( llegar, lle ) : True
'IsSuffix( calle, lle ) : True
'IsSuffix( llegar, lle ) : False

Comentários

Cada cadeia de caracteres começa e termina com uma subcadeia de caracteres vazia (""); portanto, se for uma cadeia de caracteres prefix vazia, esse método retornará true.

Note

Quando possível, você deve chamar métodos de comparação de cadeia de caracteres que têm um parâmetro de tipo CompareOptions para especificar o tipo de comparação esperado. Como regra geral, use opções linguísticas (usando a cultura atual) para comparar cadeias de caracteres exibidas na interface do usuário e especificar CompareOptions.Ordinal ou CompareOptions.OrdinalIgnoreCase para comparações de segurança.

Confira também

Aplica-se a

IsPrefix(String, String, CompareOptions)

Determina se a cadeia de caracteres de origem especificada começa com o prefixo especificado usando o valor especificado CompareOptions .

public:
 virtual bool IsPrefix(System::String ^ source, System::String ^ prefix, System::Globalization::CompareOptions options);
public virtual bool IsPrefix(string source, string prefix, System.Globalization.CompareOptions options);
abstract member IsPrefix : string * string * System.Globalization.CompareOptions -> bool
override this.IsPrefix : string * string * System.Globalization.CompareOptions -> bool
Public Overridable Function IsPrefix (source As String, prefix As String, options As CompareOptions) As Boolean

Parâmetros

source
String

A cadeia de caracteres na qual pesquisar.

prefix
String

A cadeia de caracteres a ser comparada com o início de source.

options
CompareOptions

Um valor que define como source e prefix deve ser comparado. optionsé o valor Ordinalde enumeração ou uma combinação bit a bit de um ou mais dos seguintes valores: IgnoreCase, , IgnoreSymbols, IgnoreNonSpacee IgnoreWidthIgnoreKanaType.

Retornos

true se o comprimento for prefix menor ou igual ao comprimento de source e source começar com prefix; caso contrário, false.

Exceções

source é null.

-ou-

prefix é null.

options contém um valor inválido CompareOptions .

Exemplos

O exemplo a seguir determina se uma cadeia de caracteres é o prefixo ou sufixo de outra cadeia de caracteres usando CompareOptions.

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;

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


/*
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

*/
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

End Class


'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

Comentários

Cada cadeia de caracteres começa e termina com uma subcadeia de caracteres vazia (""); portanto, se for uma cadeia de caracteres prefix vazia, esse método retornará true.

Os CompareOptions.NumericOrdering valores e os valores CompareOptions.StringSort não são válidos para esse método.

Note

Quando possível, você deve chamar métodos de comparação de cadeia de caracteres que têm um parâmetro de tipo CompareOptions para especificar o tipo de comparação esperado. Como regra geral, use opções linguísticas (usando a cultura atual) para comparar cadeias de caracteres exibidas na interface do usuário e especificar CompareOptions.Ordinal ou CompareOptions.OrdinalIgnoreCase para comparações de segurança.

Confira também

Aplica-se a