Olvasás angol nyelven Szerkesztés

Megosztás a következőn keresztül:


CompareInfo.IsPrefix Method

Definition

Determines whether a string starts with a specific prefix.

Overloads

IsPrefix(String, String)

Determines whether the specified source string starts with the specified prefix.

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

Determines whether a read-only span of characters starts with a specific prefix.

IsPrefix(String, String, CompareOptions)

Determines whether the specified source string starts with the specified prefix using the specified CompareOptions value.

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

Determines whether a string starts with a specific prefix.

IsPrefix(String, String)

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

Determines whether the specified source string starts with the specified prefix.

public virtual bool IsPrefix (string source, string prefix);
public bool IsPrefix (string source, string prefix);

Parameters

source
String

The string to search in.

prefix
String

The string to compare with the beginning of source.

Returns

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

Exceptions

source is null.

-or-

prefix is null.

Examples

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

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 prefix is an empty string, this method returns true.

Megjegyzés

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 9 és más verziók
Termék Verziók
.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
.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

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

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

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

Parameters

source
ReadOnlySpan<Char>

The read-only span of characters to search within.

prefix
ReadOnlySpan<Char>

The prefix to attempt to match at the start of source.

options
CompareOptions

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

Returns

true if prefix occurs at the start of source; otherwise, false.

Exceptions

options contains an unsupported combination of flags.

Applies to

.NET 9 és más verziók
Termék Verziók
.NET 5, 6, 7, 8, 9

IsPrefix(String, String, CompareOptions)

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

Determines whether the specified source string starts with the specified prefix using the specified CompareOptions value.

public virtual bool IsPrefix (string source, string prefix, System.Globalization.CompareOptions options);
public bool IsPrefix (string source, string prefix, System.Globalization.CompareOptions options);

Parameters

source
String

The string to search in.

prefix
String

The string to compare with the beginning of source.

options
CompareOptions

A value that defines how source and prefix should be compared. 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.

Returns

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

Exceptions

source is null.

-or-

prefix 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.

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 prefix is an empty string, this method returns true.

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

Megjegyzés

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 9 és más verziók
Termék Verziók
.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
.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

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

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

Determines whether a string starts with a specific prefix.

public bool IsPrefix (ReadOnlySpan<char> source, ReadOnlySpan<char> prefix, System.Globalization.CompareOptions options, out int matchLength);

Parameters

source
ReadOnlySpan<Char>

The read-only span of characters to search within.

prefix
ReadOnlySpan<Char>

The read-only span of characters containing the prefix to attempt to match at the start 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 prefix. This may be different than the length of prefix if a linguistic comparison is performed. Set to 0 if the prefix did not match.

Returns

true if prefix occurs at the start of source; otherwise, false.

Exceptions

options contains an unsupported combination of flags.

Remarks

This method has greater overhead than other IsPrefix(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 9 és más verziók
Termék Verziók
.NET 5, 6, 7, 8, 9