本文整理汇总了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;
}
}
输出:
The original strings: --------------------- This is bold textThis 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));
}
}
输出:
'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();
}
}
}
输出:
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
*/
示例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\"");
}
}
}