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


C# String.StartsWith方法代码示例

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


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

示例1: Main

//引入命名空间
using System;

public class Example 
{
   public static void Main() {
      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 begins with a lesser than symbol, it should not be modified" };

      // Display the initial string array.
      Console.WriteLine("The original strings:");
      Console.WriteLine("---------------------");
      foreach (var s in strSource)
         Console.WriteLine(s);
      Console.WriteLine();

      Console.WriteLine("Strings after starting tags have been stripped:");
      Console.WriteLine("-----------------------------------------------");

      // Display the strings with starting tags removed.
     foreach (var s in strSource)
        Console.WriteLine(StripStartTags(s));
   }

   private static string StripStartTags(string item) 
   {
      // Determine whether a tag begins the string.
      if (item.Trim().StartsWith("<")) {
         // Find the closing tag.
         int lastLocation = item.IndexOf( ">" );
         // Remove the tag.
         if (lastLocation >= 0) {
            item =  item.Substring( lastLocation + 1 );
      
            // Remove any additional starting tags.
            item = StripStartTags(item);
         }   
      }

      return item;
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:44,代码来源:String.StartsWith

输出:

The original strings:
---------------------
This is bold text

This is large Text

This has multiple tags This has embedded tags. This is large Text This has multiple tags This has embedded tags.

示例2: Main

//引入命名空间
using System;

public class Example
{
   public static void Main()
   {
      String title = "The House of the Seven Gables";
      String searchString = "the";
      StringComparison comparison = StringComparison.InvariantCulture;
      Console.WriteLine("'{0}':", title);
      Console.WriteLine("   Starts with '{0}' ({1:G} comparison): {2}",
                        searchString, comparison,
                        title.StartsWith(searchString, comparison));

      comparison = StringComparison.InvariantCultureIgnoreCase;
      Console.WriteLine("   Starts with '{0}' ({1:G} comparison): {2}",
                        searchString, comparison,
                        title.StartsWith(searchString, comparison));
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:21,代码来源:String.StartsWith

输出:

'The House of the Seven Gables':
Starts with 'the' (InvariantCulture comparison): False
Starts with 'the' (InvariantCultureIgnoreCase comparison): True

示例3: Main

//引入命名空间
using System;

public class Example
{
   public static void Main()
   {
      string[,] strings = { {"ABCdef", "abc" },                    
                            {"ABCdef", "abc" },  
                            {"œil","oe" },
                            { "læring}", "lae" } };
      for (int ctr1 = strings.GetLowerBound(0); ctr1 <= strings.GetUpperBound(0); ctr1++)
      {
            foreach (string cmpName in Enum.GetNames(typeof(StringComparison)))
            { 
               StringComparison strCmp = (StringComparison) Enum.Parse(typeof(StringComparison), 
                                                      cmpName);
               string instance = strings[ctr1, 0];
               string value = strings[ctr1, 1];
               Console.WriteLine("{0} starts with {1}: {2} ({3} comparison)", 
                                 instance, value, 
                                 instance.StartsWith(value, strCmp), 
                                 strCmp); 
            }
            Console.WriteLine();   
      }   
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:28,代码来源:String.StartsWith

输出:

ABCdef starts with abc: False (CurrentCulture comparison)
ABCdef starts with abc: True (CurrentCultureIgnoreCase comparison)
ABCdef starts with abc: False (InvariantCulture comparison)
ABCdef starts with abc: True (InvariantCultureIgnoreCase comparison)
ABCdef starts with abc: False (Ordinal comparison)
ABCdef starts with abc: True (OrdinalIgnoreCase comparison)

ABCdef starts with abc: False (CurrentCulture comparison)
ABCdef starts with abc: True (CurrentCultureIgnoreCase comparison)
ABCdef starts with abc: False (InvariantCulture comparison)
ABCdef starts with abc: True (InvariantCultureIgnoreCase comparison)
ABCdef starts with abc: False (Ordinal comparison)
ABCdef starts with abc: True (OrdinalIgnoreCase comparison)

œil starts with oe: True (CurrentCulture comparison)
œil starts with oe: True (CurrentCultureIgnoreCase comparison)
œil starts with oe: True (InvariantCulture comparison)
œil starts with oe: True (InvariantCultureIgnoreCase comparison)
œil starts with oe: False (Ordinal comparison)
œil starts with oe: False (OrdinalIgnoreCase comparison)

læring} starts with lae: True (CurrentCulture comparison)
læring} starts with lae: True (CurrentCultureIgnoreCase comparison)
læring} starts with lae: True (InvariantCulture comparison)
læring} starts with lae: True (InvariantCultureIgnoreCase comparison)
læring} starts with lae: False (Ordinal comparison)
læring} starts with lae: False (OrdinalIgnoreCase comparison)

示例4: Main

// This code example demonstrates the 
// System.String.StartsWith(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 aRingXYZ = "\u0061\u030a" + "xyz";

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

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

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

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

    Console.WriteLine("Case insensitive:");
    result = aRingXYZ.StartsWith(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 = aRingXYZ.StartsWith(capitalARing, false, ci);
    Console.WriteLine(msg3, result);

    Console.WriteLine("Case insensitive:");
    result = aRingXYZ.StartsWith(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).

Search for the target string "Å" in the string "a°xyz".

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,代码行数:79,代码来源:String.StartsWith

示例5: String.StartsWith()

//引入命名空间
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.StartsWith


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