Прочитај на енглеском Уреди

Делите путем


CompareInfo.IsSuffix Method

Definition

Determines whether a string ends with a specific suffix.

Overloads

IsSuffix(String, String)

Determines whether the specified source string ends with the specified suffix.

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

Determines whether a read-only span of characters ends with a specific suffix.

IsSuffix(String, String, CompareOptions)

Determines whether the specified source string ends with the specified suffix using the specified CompareOptions value.

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

Determines whether a string ends with a specific suffix.

IsSuffix(String, String)

Source:
CompareInfo.cs
Source:
CompareInfo.cs
Source:
CompareInfo.cs

Determines whether the specified source string ends with the specified suffix.

C#
public virtual bool IsSuffix(string source, string suffix);
C#
public bool IsSuffix(string source, string suffix);

Parameters

source
String

The string to search in.

suffix
String

The string to compare with the end of source.

Returns

true if the length of suffix is less than or equal to the length of source and source ends with suffix; otherwise, false.

Exceptions

source is null.

-or-

suffix is null.

Examples

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

C#
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

*/

Remarks

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

Напомена

When possible, you should call string comparison methods that have a parameter of type CompareOptions to specify the kind of comparison expected. 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.

See also

Applies to

.NET 10 и друге верзије
Производ Верзије
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

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

Source:
CompareInfo.cs
Source:
CompareInfo.cs
Source:
CompareInfo.cs

Determines whether a read-only span of characters ends with a specific suffix.

C#
public bool IsSuffix(ReadOnlySpan<char> source, ReadOnlySpan<char> suffix, System.Globalization.CompareOptions options = System.Globalization.CompareOptions.None);

Parameters

source
ReadOnlySpan<Char>

The read-only span of characters to search within.

suffix
ReadOnlySpan<Char>

The suffix to attempt to match at the end of source.

options
CompareOptions

An optional combination of CompareOptions enumeration values to use during the match. The default value is None.

Returns

true if suffix occurs at the end of source; otherwise, false.

Exceptions

options contains an unsupported combination of flags.

Applies to

.NET 10 и друге верзије
Производ Верзије
.NET 5, 6, 7, 8, 9, 10

IsSuffix(String, String, CompareOptions)

Source:
CompareInfo.cs
Source:
CompareInfo.cs
Source:
CompareInfo.cs

Determines whether the specified source string ends with the specified suffix using the specified CompareOptions value.

C#
public virtual bool IsSuffix(string source, string suffix, System.Globalization.CompareOptions options);
C#
public bool IsSuffix(string source, string suffix, System.Globalization.CompareOptions options);

Parameters

source
String

The string to search in.

suffix
String

The string to compare with the end of source.

options
CompareOptions

A value that defines how source and suffix should be compared. options is either the enumeration value Ordinal used by itself, or the bitwise combination of one or more of the following values: IgnoreCase, IgnoreSymbols, IgnoreNonSpace, IgnoreWidth, and IgnoreKanaType.

Returns

true if the length of suffix is less than or equal to the length of source and source ends with suffix; otherwise, false.

Exceptions

source is null.

-or-

suffix is null.

options contains an invalid CompareOptions value.

Examples

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

C#
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

*/

Remarks

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

The CompareOptions.StringSort value is not valid for this method.

Напомена

When possible, you should call string comparison methods that have a parameter of type CompareOptions to specify the kind of comparison expected. 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.

See also

Applies to

.NET 10 и друге верзије
Производ Верзије
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

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

Source:
CompareInfo.cs
Source:
CompareInfo.cs
Source:
CompareInfo.cs

Determines whether a string ends with a specific suffix.

C#
public bool IsSuffix(ReadOnlySpan<char> source, ReadOnlySpan<char> suffix, System.Globalization.CompareOptions options, out int matchLength);

Parameters

source
ReadOnlySpan<Char>

The read-only span of characters to search within.

suffix
ReadOnlySpan<Char>

The read-only span of characters containing the suffix to attempt to match at the end of source.

options
CompareOptions

The CompareOptions to use during the match.

matchLength
Int32

When this method returns, contains the number of characters of source that matched the desired suffix. This may be different than the length of suffix if a linguistic comparison is performed. Set to 0 if the suffix did not match.

Returns

true if suffix occurs at the end of source; otherwise, false.

Exceptions

options contains an unsupported combination of flags.

Remarks

This method has greater overhead than other IsSuffix(String, String, CompareOptions) overloads that don't take a matchLength argument. Call this overload only if you require the match length information.

Applies to

.NET 10 и друге верзије
Производ Верзије
.NET 5, 6, 7, 8, 9, 10