CompareInfo.IsPrefix 方法

定義

判斷字串是否以特定字首開始。

多載

IsPrefix(String, String)

判斷指定的來源字串是否以指定字首開始。

IsPrefix(ReadOnlySpan<Char>, ReadOnlySpan<Char>, CompareOptions)

判斷唯讀字元範圍是否以特定字首開始。

IsPrefix(String, String, CompareOptions)

使用指定的 CompareOptions 值,判斷指定的來源字串是否以指定字首開始。

IsPrefix(ReadOnlySpan<Char>, ReadOnlySpan<Char>, CompareOptions, Int32)

判斷字串是否以特定字首開始。

IsPrefix(String, String)

來源:
CompareInfo.cs
來源:
CompareInfo.cs
來源:
CompareInfo.cs

判斷指定的來源字串是否以指定字首開始。

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

參數

source
String

要在其中搜尋的字串。

prefix
String

要與 source 的開頭相比較的字串。

傳回

如果 prefix 的長度小於或等於 source 的長度,且 source 是以 prefix 開始,則為 true,否則為 false

例外狀況

sourcenull

-或-

prefixnull

範例

下列範例會判斷字串是另一個字串的前置詞或尾碼。

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

備註

每個字串都會以空的子字串開頭和結尾 (「」) ;因此,如果 prefix 是空字串,這個方法會傳 true 回 。

注意

可能的話,您應該呼叫具有 型 CompareOptions 別參數的字串比較方法,以指定預期的比較類型。 一般規則是,使用語言選項 (使用目前的文化特性) 來比較使用者介面中顯示的字串,並指定 CompareOptions.OrdinalCompareOptions.OrdinalIgnoreCase 進行安全性比較。

另請參閱

適用於

IsPrefix(ReadOnlySpan<Char>, ReadOnlySpan<Char>, CompareOptions)

來源:
CompareInfo.cs
來源:
CompareInfo.cs
來源:
CompareInfo.cs

判斷唯讀字元範圍是否以特定字首開始。

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

參數

source
ReadOnlySpan<Char>

要在其中搜尋之字元的唯讀範圍。

prefix
ReadOnlySpan<Char>

要嘗試在 source 開頭比對的字首。

options
CompareOptions

要在比對期間使用的選擇性 CompareOptions 列舉值組合。 預設值是 None

傳回

prefix 出現在 source 的開頭,則為 true;否則為 false

例外狀況

options 包含不支援的旗標組合。

適用於

IsPrefix(String, String, CompareOptions)

來源:
CompareInfo.cs
來源:
CompareInfo.cs
來源:
CompareInfo.cs

使用指定的 CompareOptions 值,判斷指定的來源字串是否以指定字首開始。

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

參數

source
String

要在其中搜尋的字串。

prefix
String

要與 source 的開頭相比較的字串。

options
CompareOptions

值,這個值會定義應該如何比較 sourceprefixoptions 若不是 Ordinal 列舉值,就是下列一個或多個值的位元組合:IgnoreCaseIgnoreSymbolsIgnoreNonSpaceIgnoreWidthIgnoreKanaType

傳回

如果 prefix 的長度小於或等於 source 的長度,且 source 是以 prefix 開始,則為 true,否則為 false

例外狀況

sourcenull

-或-

prefixnull

options 包含無效的 CompareOptions 值。

範例

下列範例會使用 判斷字串是另一個字串 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

備註

每個字串都會以空的子字串開頭和結尾 (「」) ;因此,如果 prefix 是空字串,這個方法會傳 true 回 。

這個 CompareOptions.StringSort 方法的值無效。

注意

可能的話,您應該呼叫具有 型 CompareOptions 別參數的字串比較方法,以指定預期的比較類型。 一般規則是,使用語言選項 (使用目前的文化特性) 來比較使用者介面中顯示的字串,並指定 CompareOptions.OrdinalCompareOptions.OrdinalIgnoreCase 進行安全性比較。

另請參閱

適用於

IsPrefix(ReadOnlySpan<Char>, ReadOnlySpan<Char>, CompareOptions, Int32)

來源:
CompareInfo.cs
來源:
CompareInfo.cs
來源:
CompareInfo.cs

判斷字串是否以特定字首開始。

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

參數

source
ReadOnlySpan<Char>

要在其中搜尋之字元的唯讀範圍。

prefix
ReadOnlySpan<Char>

字元唯讀範圍包含嘗試符合 source 開頭的前置詞。

options
CompareOptions

比對期間所使用的 CompareOptions

matchLength
Int32

當此方法傳回時,包含符合所需前置詞的 source 字元數。 如果執行語言比較,這可能會與 prefix 的長度不同。 如果前置詞不相符,請設定為 0。

傳回

prefix 出現在 source 的開頭,則為 true;否則為 false

例外狀況

options 包含不支援的旗標組合。

備註

這個方法的額外負荷大於不採用 matchLength 引數的其他 IsPrefix(String, String, CompareOptions) 多載。 只有在您需要相符長度資訊時,才呼叫此多載。

適用於