CompareInfo.IsPrefix Método
Definição
Determina se uma cadeia de caracteres começa com um prefixo específico.Determines whether a string starts with a specific prefix.
Sobrecargas
| IsPrefix(String, String) |
Determina se a cadeia de caracteres de origem determinada começa com o prefixo especificado.Determines whether the specified source string starts with the specified prefix. |
| IsPrefix(ReadOnlySpan<Char>, ReadOnlySpan<Char>, CompareOptions) |
Determina se um intervalo de caracteres somente leitura começa com um prefixo específico.Determines whether a read-only span of characters starts with a specific prefix. |
| IsPrefix(String, String, CompareOptions) |
Determina se a cadeia de caracteres de origem determinada começa com o prefixo dado usando o valor CompareOptions especificado.Determines whether the specified source string starts with the specified prefix using the specified CompareOptions value. |
| IsPrefix(ReadOnlySpan<Char>, ReadOnlySpan<Char>, CompareOptions, Int32) |
Determina se uma cadeia de caracteres começa com um prefixo específico.Determines whether a string starts with a specific prefix. |
IsPrefix(String, String)
Determina se a cadeia de caracteres de origem determinada começa com o prefixo especificado.Determines whether the specified source string starts with the specified prefix.
public:
virtual bool IsPrefix(System::String ^ source, System::String ^ prefix);
public:
bool IsPrefix(System::String ^ source, System::String ^ prefix);
public virtual bool IsPrefix (string source, string prefix);
public bool IsPrefix (string source, string prefix);
abstract member IsPrefix : string * string -> bool
override this.IsPrefix : string * string -> bool
member this.IsPrefix : string * string -> bool
Public Overridable Function IsPrefix (source As String, prefix As String) As Boolean
Public Function IsPrefix (source As String, prefix As String) As Boolean
Parâmetros
- source
- String
A cadeia de caracteres a ser pesquisada.The string to search in.
- prefix
- String
A cadeia de caracteres a ser comparada com o início de source.The string to compare with the beginning of source.
Retornos
true se o tamanho de prefix for menor ou igual ao tamanho de source e source começar com prefix; caso contrário, false.true if the length of prefix is less than or equal to the length of source and source starts with prefix; otherwise, false.
Exceções
Exemplos
O exemplo a seguir determina se uma cadeia de caracteres é o prefixo ou sufixo de outra cadeia de caracteres.The following example determines whether a string is the prefix or suffix of another string.
using namespace System;
using namespace System::Globalization;
int 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 S"calle" and S"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 S"calle" and S"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
*/
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 prefix for uma cadeia de caracteres vazia, esse método retornará true .Every string starts and ends with an empty substring (""); therefore, if prefix is an empty string, this method returns true.
Observação
Quando possível, você deve chamar métodos de comparação de cadeia de caracteres que têm um parâmetro do tipo CompareOptions para especificar o tipo de comparação esperado.When possible, you should call string comparison methods that have a parameter of type CompareOptions to specify the kind of comparison expected. Como regra geral, use as opções linguísticas (usando a cultura atual) para comparar as cadeias de caracteres exibidas na interface do usuário e especificar CompareOptions.Ordinal ou CompareOptions.OrdinalIgnoreCase para comparações de segurança.As a general rule, use linguistic options (using the current culture) for comparing strings displayed in the user interface and specify CompareOptions.Ordinal or CompareOptions.OrdinalIgnoreCase for security comparisons.
Confira também
Aplica-se a
IsPrefix(ReadOnlySpan<Char>, ReadOnlySpan<Char>, CompareOptions)
Determina se um intervalo de caracteres somente leitura começa com um prefixo específico.Determines whether a read-only span of characters starts with a specific prefix.
public bool IsPrefix (ReadOnlySpan<char> source, ReadOnlySpan<char> prefix, System.Globalization.CompareOptions options = System.Globalization.CompareOptions.None);
member this.IsPrefix : ReadOnlySpan<char> * ReadOnlySpan<char> * System.Globalization.CompareOptions -> bool
Public Function IsPrefix (source As ReadOnlySpan(Of Char), prefix As ReadOnlySpan(Of Char), Optional options As CompareOptions = System.Globalization.CompareOptions.None) As Boolean
Parâmetros
- source
- ReadOnlySpan<Char>
O intervalo somente leitura de caracteres a ser pesquisado.The read-only span of characters to search within.
- prefix
- ReadOnlySpan<Char>
O prefixo para tentar corresponder ao início de source.The prefix to attempt to match at the start of source.
- options
- CompareOptions
Uma combinação opcional de valores de enumeração CompareOptions a serem usados durante a correspondência.An optional combination of CompareOptions enumeration values to use during the match. O valor padrão é None.The default value is None.
Retornos
true se prefix ocorrer no início de source; caso contrário, false.true if prefix occurs at the start of source; otherwise, false.
Exceções
options contém uma combinação não compatível de sinalizadores.options contains an unsupported combination of flags.
Aplica-se a
IsPrefix(String, String, CompareOptions)
Determina se a cadeia de caracteres de origem determinada começa com o prefixo dado usando o valor CompareOptions especificado.Determines whether the specified source string starts with the specified prefix using the specified CompareOptions value.
public:
virtual bool IsPrefix(System::String ^ source, System::String ^ prefix, System::Globalization::CompareOptions options);
public:
bool IsPrefix(System::String ^ source, System::String ^ prefix, System::Globalization::CompareOptions options);
public virtual bool IsPrefix (string source, string prefix, System.Globalization.CompareOptions options);
public 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
member this.IsPrefix : string * string * System.Globalization.CompareOptions -> bool
Public Overridable Function IsPrefix (source As String, prefix As String, options As CompareOptions) As Boolean
Public Function IsPrefix (source As String, prefix As String, options As CompareOptions) As Boolean
Parâmetros
- source
- String
A cadeia de caracteres a ser pesquisada.The string to search in.
- prefix
- String
A cadeia de caracteres a ser comparada com o início de source.The string to compare with the beginning of source.
- options
- CompareOptions
Um valor que define como source e prefix devem ser comparados.A value that defines how source and prefix should be compared. options é o valor de enumeração Ordinal ou uma combinação bit a bit de um ou mais dos seguintes valores: IgnoreCase, IgnoreSymbols, IgnoreNonSpace, IgnoreWidth e IgnoreKanaType.options is either the enumeration value Ordinal, or a bitwise combination of one or more of the following values: IgnoreCase, IgnoreSymbols, IgnoreNonSpace, IgnoreWidth, and IgnoreKanaType.
Retornos
true se o tamanho de prefix for menor ou igual ao tamanho de source e source começar com prefix; caso contrário, false.true if the length of prefix is less than or equal to the length of source and source starts with prefix; otherwise, false.
Exceções
options contém um valor CompareOptions inválido.options contains an invalid CompareOptions value.
Exemplos
O exemplo a seguir determina se uma cadeia de caracteres é o prefixo ou sufixo de outra cadeia de caracteres usando CompareOptions .The following example determines whether a string is the prefix or suffix of another string using CompareOptions.
using namespace System;
using namespace System::Globalization;
int 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
*/
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 prefix for uma cadeia de caracteres vazia, esse método retornará true .Every string starts and ends with an empty substring (""); therefore, if prefix is an empty string, this method returns true.
O CompareOptions.StringSort valor não é válido para este método.The CompareOptions.StringSort value is not valid for this method.
Observação
Quando possível, você deve chamar métodos de comparação de cadeia de caracteres que têm um parâmetro do tipo CompareOptions para especificar o tipo de comparação esperado.When possible, you should call string comparison methods that have a parameter of type CompareOptions to specify the kind of comparison expected. Como regra geral, use as opções linguísticas (usando a cultura atual) para comparar as cadeias de caracteres exibidas na interface do usuário e especificar CompareOptions.Ordinal ou CompareOptions.OrdinalIgnoreCase para comparações de segurança.As a general rule, use linguistic options (using the current culture) for comparing strings displayed in the user interface and specify CompareOptions.Ordinal or CompareOptions.OrdinalIgnoreCase for security comparisons.
Confira também
Aplica-se a
IsPrefix(ReadOnlySpan<Char>, ReadOnlySpan<Char>, CompareOptions, Int32)
Determina se uma cadeia de caracteres começa com um prefixo específico.Determines whether a string starts with a specific prefix.
public:
bool IsPrefix(ReadOnlySpan<char> source, ReadOnlySpan<char> prefix, System::Globalization::CompareOptions options, [Runtime::InteropServices::Out] int % matchLength);
public bool IsPrefix (ReadOnlySpan<char> source, ReadOnlySpan<char> prefix, System.Globalization.CompareOptions options, out int matchLength);
member this.IsPrefix : ReadOnlySpan<char> * ReadOnlySpan<char> * System.Globalization.CompareOptions * int -> bool
Public Function IsPrefix (source As ReadOnlySpan(Of Char), prefix As ReadOnlySpan(Of Char), options As CompareOptions, ByRef matchLength As Integer) As Boolean
Parâmetros
- source
- ReadOnlySpan<Char>
O intervalo somente leitura de caracteres a ser pesquisado.The read-only span of characters to search within.
- prefix
- ReadOnlySpan<Char>
O intervalo somente leitura de caracteres que contém o prefixo para tentar fazer a correspondência com o início de source.The read-only span of characters containing the prefix to attempt to match at the start of source.
- options
- CompareOptions
O CompareOptions a ser usado durante a correspondência.The CompareOptions to use during the match.
- matchLength
- Int32
Quando este método retorna, ele contém o número de caracteres de source correspondentes ao prefixo desejado.When this method returns, contains the number of characters of source that matched the desired prefix. Ele poderá ser diferente do comprimento de prefix se uma comparação linguística for executada.This may be different than the length of prefix if a linguistic comparison is performed. Defina como 0 se o prefixo não for correspondente.Set to 0 if the prefix did not match.
Retornos
true se prefix ocorrer no início de source; caso contrário, false.true if prefix occurs at the start of source; otherwise, false.
Exceções
options contém uma combinação não compatível de sinalizadores.options contains an unsupported combination of flags.
Comentários
Esse método tem uma sobrecarga maior do que outras IsPrefix(String, String, CompareOptions) sobrecargas que não assumem um matchLength argumento.This method has greater overhead than other IsPrefix(String, String, CompareOptions) overloads that don't take a matchLength argument. Chame essa sobrecarga somente se você precisar das informações de comprimento de correspondência.Call this overload only if you require the match length information.