CompareInfo.IsSuffix 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
确定字符串是否以特定的后缀结尾。
重载
IsSuffix(String, String) |
确定指定的源字符串是否以指定的后缀结尾。 |
IsSuffix(ReadOnlySpan<Char>, ReadOnlySpan<Char>, CompareOptions) |
确定某一字符只读范围是否以指定后缀结尾。 |
IsSuffix(String, String, CompareOptions) |
使用指定的 CompareOptions 值确定指定的源字符串是否以指定的后缀结尾。 |
IsSuffix(ReadOnlySpan<Char>, ReadOnlySpan<Char>, CompareOptions, Int32) |
确定字符串是否以特定的后缀结尾。 |
IsSuffix(String, String)
- Source:
- CompareInfo.cs
- Source:
- CompareInfo.cs
- Source:
- CompareInfo.cs
确定指定的源字符串是否以指定的后缀结尾。
public:
virtual bool IsSuffix(System::String ^ source, System::String ^ suffix);
public:
bool IsSuffix(System::String ^ source, System::String ^ suffix);
public virtual bool IsSuffix (string source, string suffix);
public bool IsSuffix (string source, string suffix);
abstract member IsSuffix : string * string -> bool
override this.IsSuffix : string * string -> bool
member this.IsSuffix : string * string -> bool
Public Overridable Function IsSuffix (source As String, suffix As String) As Boolean
Public Function IsSuffix (source As String, suffix As String) As Boolean
参数
- source
- String
要在其中搜索的字符串。
- suffix
- String
要与 source
的结尾进行比较的字符串。
返回
如果 suffix
的长度小于或等于 source
的长度,并且 source
以 suffix
结尾,则为 true
;否则为 false
。
例外
示例
以下示例确定字符串是另一个字符串的前缀还是后缀。
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
注解
每个字符串以空子字符串 (“”) 开头和结尾;因此,如果 suffix
是空字符串,则此方法返回 true
。
注意
如果可能,应调用具有 类型 CompareOptions 参数的字符串比较方法,以指定预期的比较类型。 一般情况下,使用语言选项 (使用当前区域性) 来比较用户界面中显示的字符串,并指定 CompareOptions.Ordinal 或 CompareOptions.OrdinalIgnoreCase 进行安全比较。
另请参阅
适用于
IsSuffix(ReadOnlySpan<Char>, ReadOnlySpan<Char>, CompareOptions)
- Source:
- CompareInfo.cs
- Source:
- CompareInfo.cs
- Source:
- CompareInfo.cs
确定某一字符只读范围是否以指定后缀结尾。
public bool IsSuffix (ReadOnlySpan<char> source, ReadOnlySpan<char> suffix, System.Globalization.CompareOptions options = System.Globalization.CompareOptions.None);
member this.IsSuffix : ReadOnlySpan<char> * ReadOnlySpan<char> * System.Globalization.CompareOptions -> bool
Public Function IsSuffix (source As ReadOnlySpan(Of Char), suffix As ReadOnlySpan(Of Char), Optional options As CompareOptions = System.Globalization.CompareOptions.None) As Boolean
参数
- source
- ReadOnlySpan<Char>
要在其中搜索的字符只读范围。
- suffix
- ReadOnlySpan<Char>
要尝试与 source
结尾相匹配的后缀。
- options
- CompareOptions
要在匹配过程中使用的 CompareOptions 枚举值的可选组合。 默认值为 None。
返回
如果 suffix
出现在 source
的结尾,则为 true
;否则为 false
。
例外
options
包含不受支持的标志组合。
适用于
IsSuffix(String, String, CompareOptions)
- Source:
- CompareInfo.cs
- Source:
- CompareInfo.cs
- Source:
- CompareInfo.cs
使用指定的 CompareOptions 值确定指定的源字符串是否以指定的后缀结尾。
public:
virtual bool IsSuffix(System::String ^ source, System::String ^ suffix, System::Globalization::CompareOptions options);
public:
bool IsSuffix(System::String ^ source, System::String ^ suffix, System::Globalization::CompareOptions options);
public virtual bool IsSuffix (string source, string suffix, System.Globalization.CompareOptions options);
public bool IsSuffix (string source, string suffix, System.Globalization.CompareOptions options);
abstract member IsSuffix : string * string * System.Globalization.CompareOptions -> bool
override this.IsSuffix : string * string * System.Globalization.CompareOptions -> bool
member this.IsSuffix : string * string * System.Globalization.CompareOptions -> bool
Public Overridable Function IsSuffix (source As String, suffix As String, options As CompareOptions) As Boolean
Public Function IsSuffix (source As String, suffix As String, options As CompareOptions) As Boolean
参数
- source
- String
要在其中搜索的字符串。
- suffix
- String
要与 source
的结尾进行比较的字符串。
- options
- CompareOptions
一个值,用于定义应如何比较 source
和 suffix
。 options
可以为其自身使用的枚举值 Ordinal,或为以下一个或多个值的按位组合:IgnoreCase、IgnoreSymbols、IgnoreNonSpace、IgnoreWidth 和 IgnoreKanaType。
返回
如果 suffix
的长度小于或等于 source
的长度,并且 source
以 suffix
结尾,则为 true
;否则为 false
。
例外
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
注解
每个字符串以空子字符串 (“”) 开头和结尾;因此,如果 suffix
是空字符串,则此方法返回 true
。
该值 CompareOptions.StringSort 对此方法无效。
注意
如果可能,应调用具有 类型 CompareOptions 参数的字符串比较方法,以指定预期的比较类型。 一般情况下,使用语言选项 (使用当前区域性) 来比较用户界面中显示的字符串,并指定 CompareOptions.Ordinal 或 CompareOptions.OrdinalIgnoreCase 进行安全比较。
另请参阅
适用于
IsSuffix(ReadOnlySpan<Char>, ReadOnlySpan<Char>, CompareOptions, Int32)
- Source:
- CompareInfo.cs
- Source:
- CompareInfo.cs
- Source:
- CompareInfo.cs
确定字符串是否以特定的后缀结尾。
public:
bool IsSuffix(ReadOnlySpan<char> source, ReadOnlySpan<char> suffix, System::Globalization::CompareOptions options, [Runtime::InteropServices::Out] int % matchLength);
public bool IsSuffix (ReadOnlySpan<char> source, ReadOnlySpan<char> suffix, System.Globalization.CompareOptions options, out int matchLength);
member this.IsSuffix : ReadOnlySpan<char> * ReadOnlySpan<char> * System.Globalization.CompareOptions * int -> bool
Public Function IsSuffix (source As ReadOnlySpan(Of Char), suffix As ReadOnlySpan(Of Char), options As CompareOptions, ByRef matchLength As Integer) As Boolean
参数
- source
- ReadOnlySpan<Char>
要在其中搜索的字符只读范围。
- suffix
- ReadOnlySpan<Char>
字符的只读范围,其中包含在 source
末尾要尝试匹配的后缀。
- options
- CompareOptions
要在匹配期间使用的 CompareOptions。
- matchLength
- Int32
当此方法返回时,包含与所需后缀匹配的 source
的字符数。 如果执行了语言比较,则这可能与 suffix
的长度不同。 如果后缀不匹配,则设置为 0。
返回
如果 suffix
出现在 source
的结尾,则为 true
;否则为 false
。
例外
options
包含不受支持的标志组合。
注解
此方法的开销大于不采用matchLength
参数的其他IsSuffix(String, String, CompareOptions)重载。 仅当需要匹配长度信息时,才调用此重载。