当前位置: 首页>>代码示例>>C#>>正文


C# String.EndsWith方法代码示例

本文整理汇总了C#中System.String.EndsWith方法的典型用法代码示例。如果您正苦于以下问题:C# String.EndsWith方法的具体用法?C# String.EndsWith怎么用?C# String.EndsWith使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.String的用法示例。


在下文中一共展示了String.EndsWith方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Main

//引入命名空间
using System;

public class Example
{
   public static void Main()
   {
      String[] strings = { "This is a string.", "Hello!", "Nothing.", 
                           "Yes.", "randomize" };
      foreach (var value in strings) {
         bool endsInPeriod = value.EndsWith(".");
         Console.WriteLine("'{0}' ends in a period: {1}", 
                           value, endsInPeriod);
      }                            
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:16,代码来源:String.EndsWith

输出:

'This is a string.' ends in a period: True
'Hello!' ends in a period: False
'Nothing.' ends in a period: True
'Yes.' ends in a period: True
'randomize' ends in a period: False

示例2: Main

//引入命名空间
using System;

public class EndsWithTest {
    public static void Main() {

        // process an input file that contains html tags.
        // this sample checks for multiple tags at the end of the line, rather than simply
        // removing the last one.
        // note: HTML markup tags always end in a greater than symbol (>).

        string [] strSource = { "<b>This is bold text</b>", "<H1>This is large Text</H1>",
                "<b><i><font color=green>This has multiple tags</font></i></b>",
                "<b>This has <i>embedded</i> tags.</b>",
                "This line simply ends with a greater than symbol, it should not be modified>" };

        Console.WriteLine("The following lists the items before the ends have been stripped:");
        Console.WriteLine("-----------------------------------------------------------------");

        // print out the initial array of strings
        foreach ( string s in strSource )
            Console.WriteLine( s );

        Console.WriteLine();

        Console.WriteLine("The following lists the items after the ends have been stripped:");
        Console.WriteLine("----------------------------------------------------------------");

        // print out the array of strings
        foreach (var s in strSource)
            Console.WriteLine(StripEndTags(s));
    }

    private static string StripEndTags( string item ) {

        bool found = false;
            
        // try to find a tag at the end of the line using EndsWith
        if (item.Trim().EndsWith(">")) {
                
            // now search for the opening tag...
            int lastLocation = item.LastIndexOf( "</" );

            // remove the identified section, if it is a valid region
            if ( lastLocation >= 0 ) {
                found = true;
                item =  item.Substring( 0, lastLocation );
            }    
        }

        if (found)
           item = StripEndTags(item);
           
        return item;
    }
}
开发者ID:.NET开发者,项目名称:System,代码行数:56,代码来源:String.EndsWith

输出:

The following lists the items before the ends have been stripped:
-----------------------------------------------------------------
This is bold text

This is large Text

This has multiple tags This has embedded tags. This line simply ends with a greater than symbol, it should not be modified> The following lists the items after the ends have been stripped: ---------------------------------------------------------------- This is bold text

This is large Text This has multiple tags This has embedded tags. This line simply ends with a greater than symbol, it should not be modified>


示例3: Main

// This example demonstrates the 
// System.String.EndsWith(String, StringComparison) method.

using System;
using System.Threading;

class Sample 
{
    public static void Main() 
    {
    string intro = "Determine whether a string ends with another string, " +
                   "using\n  different values of StringComparison.";

    StringComparison[] scValues = {
        StringComparison.CurrentCulture,
        StringComparison.CurrentCultureIgnoreCase,
        StringComparison.InvariantCulture,
        StringComparison.InvariantCultureIgnoreCase,
        StringComparison.Ordinal,
        StringComparison.OrdinalIgnoreCase };

//
    Console.Clear();
    Console.WriteLine(intro);

// Display the current culture because the culture-specific comparisons
// can produce different results with different cultures.
    Console.WriteLine("The current culture is {0}.\n", 
                       Thread.CurrentThread.CurrentCulture.Name);

// Determine whether three versions of the letter I are equal to each other. 
    foreach (StringComparison sc in scValues)
        {
        Console.WriteLine("StringComparison.{0}:", sc);
        Test("abcXYZ", "XYZ", sc);
        Test("abcXYZ", "xyz", sc);
        Console.WriteLine();
        }
    }

    protected static void Test(string x, string y, StringComparison comparison)
    {
    string resultFmt = "\"{0}\" {1} with \"{2}\".";
    string result = "does not end";
//
    if (x.EndsWith(y, comparison))
        result = "ends";
    Console.WriteLine(resultFmt, x, result, y);
    }
}
开发者ID:.NET开发者,项目名称:System,代码行数:50,代码来源:String.EndsWith

输出:

Determine whether a string ends with another string, using
  different values of StringComparison.
The current culture is en-US.

StringComparison.CurrentCulture:
"abcXYZ" ends with "XYZ".
"abcXYZ" does not end with "xyz".

StringComparison.CurrentCultureIgnoreCase:
"abcXYZ" ends with "XYZ".
"abcXYZ" ends with "xyz".

StringComparison.InvariantCulture:
"abcXYZ" ends with "XYZ".
"abcXYZ" does not end with "xyz".

StringComparison.InvariantCultureIgnoreCase:
"abcXYZ" ends with "XYZ".
"abcXYZ" ends with "xyz".

StringComparison.Ordinal:
"abcXYZ" ends with "XYZ".
"abcXYZ" does not end with "xyz".

StringComparison.OrdinalIgnoreCase:
"abcXYZ" ends with "XYZ".
"abcXYZ" ends with "xyz".

示例4: Main

// This code example demonstrates the 
// System.String.EndsWith(String, ..., CultureInfo) method.

using System;
using System.Threading;
using System.Globalization;

class Sample 
{
    public static void Main() 
    {
    string msg1 = "Search for the target string \"{0}\" in the string \"{1}\".\n";
    string msg2 = "Using the {0} - \"{1}\" culture:";
    string msg3 = "  The string to search ends with the target string: {0}";
    bool result = false;
    CultureInfo ci;

// Define a target string to search for.
// U+00c5 = LATIN CAPITAL LETTER A WITH RING ABOVE
    string capitalARing = "\u00c5";

// Define a string to search. 
// The result of combining the characters LATIN SMALL LETTER A and COMBINING 
// RING ABOVE (U+0061, U+030a) is linguistically equivalent to the character 
// LATIN SMALL LETTER A WITH RING ABOVE (U+00e5).
    string xyzARing = "xyz" + "\u0061\u030a";

// Clear the screen and display an introduction.
    Console.Clear();

// Display the string to search for and the string to search.
    Console.WriteLine(msg1, capitalARing, xyzARing);

// Search using English-United States culture.
    ci = new CultureInfo("en-US");
    Console.WriteLine(msg2, ci.DisplayName, ci.Name);

    Console.WriteLine("Case sensitive:");
    result = xyzARing.EndsWith(capitalARing, false, ci);
    Console.WriteLine(msg3, result);

    Console.WriteLine("Case insensitive:");
    result = xyzARing.EndsWith(capitalARing, true, ci);
    Console.WriteLine(msg3, result);
    Console.WriteLine();

// Search using Swedish-Sweden culture.
    ci = new CultureInfo("sv-SE");
    Console.WriteLine(msg2, ci.DisplayName, ci.Name);

    Console.WriteLine("Case sensitive:");
    result = xyzARing.EndsWith(capitalARing, false, ci);
    Console.WriteLine(msg3, result);

    Console.WriteLine("Case insensitive:");
    result = xyzARing.EndsWith(capitalARing, true, ci);
    Console.WriteLine(msg3, result);
    }
}

/*
Note: This code example was executed on a console whose user interface 
culture is "en-US" (English-United States).

This code example produces the following results:

Search for the target string "Å" in the string "xyza°".

Using the English (United States) - "en-US" culture:
Case sensitive:
  The string to search ends with the target string: False
Case insensitive:
  The string to search ends with the target string: True

Using the Swedish (Sweden) - "sv-SE" culture:
Case sensitive:
  The string to search ends with the target string: False
Case insensitive:
  The string to search ends with the target string: False

*/
开发者ID:.NET开发者,项目名称:System,代码行数:81,代码来源:String.EndsWith

示例5: String.EndsWith()

//引入命名空间
using System;

class MainClass {

    public static void Main() {
        string[] myStrings = {"To", "be", "or", "not","to", "be"};
        string myString = String.Join(".", myStrings);        
        Console.WriteLine("myString = " + myString);
        if (myString.StartsWith("To")) {
            Console.WriteLine("myString starts with \"To\"");
        }
        if (myString.EndsWith("be")) {
            Console.WriteLine("myString ends with \"be\"");
        }

    
    }
}
开发者ID:C#程序员,项目名称:System,代码行数:19,代码来源:String.EndsWith


注:本文中的System.String.EndsWith方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。