本文由机器翻译。若要查看英语原文,请勾选“英语”复选框。 也可将鼠标指针移到文本上,在弹出窗口中显示英语原文。
翻译
英语

String.StartsWith 方法 (String, Boolean, CultureInfo)

 

确定在使用指定的区域性进行比较时此字符串实例的开头是否与指定的字符串匹配。

命名空间:   System
程序集:  mscorlib(位于 mscorlib.dll)

public bool StartsWith(
	string value,
	bool ignoreCase,
	CultureInfo culture
)

参数

value
Type: System.String

要比较的字符串。

ignoreCase
Type: System.Boolean

若要在比较过程中忽略大小写,则为 true;否则为 false

culture
Type: System.Globalization.CultureInfo

确定如何对此字符串与 value 进行比较的区域性信息。 如果 culturenull,则使用当前区域性。

返回值

Type: System.Boolean

如果 true 参数与此字符串的开头匹配,则为 value;否则为 false

Exception Condition
ArgumentNullException

valuenull

此方法比较value参数是相同的长度,作为此字符串的开头处的子字符串value,并返回一个值,指示它们是否相等。 若要相等,value必须是空字符串 (String.Empty),必须是对此同一实例的引用,或者必须匹配此实例的开头。

此方法执行使用指定的大小写和区域性的比较。

下面的示例确定一个字符串是否出现在另一个字符串的开头。 StartsWith几次使用区分大小写、 区分大小写和影响搜索的结果的不同区域性调用方法。

// 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

*/

.NET Framework
自 2.0 起可用
返回页首
显示: