在C#中, StartsWith()是一个字符串方法。此方法用于检查当前字符串实例的开头是否与指定的字符串匹配。如果匹配,则返回字符串,否则返回false。使用foreach循环,可以检查许多字符串。可以通过向其传递不同类型和数量的参数来重载此方法。
String.StartsWith(String)方法
此方法用于检查字符串对象的开头是否与特定字符串匹配。如果匹配,则返回字符串,否则返回false。
句法:
public bool StartsWith(string input_string)
范围:
input_string: It is a required string which is to be compared and type of this parameter is System.String.
返回类型:该函数返回布尔值,即true,如果它发现一个匹配,否则将返回false。返回类型为System.Boolean 。
下面的程序演示了String.StartsWith(String)方法的使用:
- 程序1:
CSHARP
// C# program to illustrate the
// String.StartsWith(String) Method
using System;
public class GFG {
// Main Method
static public void Main()
{
// The input string or character
string str = "https://www.geeksforgeeks.org/placement-guide/";
// Different string character and
// Possible string to be matches
string[] m = new string[] {
"https://geeksforgeeks.org",
"https://www.geeksforgeeks.org",
"https://www.geeksforgeeks.org/placement"};
// Using foreach - loop to check
// each possible match
foreach(string s in m)
{
// To check match second possibility
if (str.StartsWith(s)) {
// Display the result
Console.WriteLine(s);
return;
}
}
}
}
CSHARP
// C# program to illustrate the
// String.StartsWith (String) Method
using System;
class Geeks {
// Main Method
public static void Main()
{
string[] input_str = {
"
GeekforGeeks Computer Science Portal
",
"GeekforGeeks Sudo Placement
",
"GeekforGeeks Placement Preparation
",
"GeekforGeeks Contribute
",
"GeekforGeeks Contribute ",
"GeekforGeeks Interview
",};
// Display result after implementation StartsWith()
// method in strings starting to be tags removed.
foreach(var n in input_str)
Console.WriteLine(htmlStartTags(n));
}
private static string htmlStartTags(string str)
{
// To check starting "<"tag is or not
if (str.Trim().StartsWith("<"))
{
// Find the closing ">" tag.
int end = str.IndexOf(">");
// After getting tag, then remove the tag
if (end >= 0)
{
str = str.Substring(end + 1);
// Additional starting tags to be romove
str = htmlStartTags(str);
}
}
return str;
}
}
CSHARP
// C# program to illustrate the
// String.StartsWith (string,
// bool, CultureInfo) Method
using System.Threading;
using System.Globalization;
using System;
class Sudo {
// Main Method
public static void Main(string[] args)
{
// Input string
string str1 = "Geeks";
// Implementaion of startswith() function
// test in original string.
bool result_a = str1.StartsWith("Geeks", false,
CultureInfo.InvariantCulture);
// test in small letter string.
bool result_b = str1.StartsWith("geeks", false,
CultureInfo.InvariantCulture);
// test in capital letter string.
bool result_c = str1.StartsWith("GEEKS", false,
CultureInfo.InvariantCulture);
// test in no string parameter .
bool result_d = str1.StartsWith(" ", false,
CultureInfo.InvariantCulture);
// Display result
Console.WriteLine(result_a);
Console.WriteLine(result_b);
Console.WriteLine(result_c);
Console.WriteLine(result_d);
}
}
CSHARP
// C# program to illustrate the
// StartsWith(String, StringComparison)
// method
using System;
class Sudo {
// Main Method
public static void Main(string[] args)
{
// Input two string
string str1 = "GeeksforGeeks";
string str2 = "Learn CSharp";
// Implementaion of startswith() function
// test for original string1 value.
bool result_a = str1.StartsWith("Geek",
StringComparison.CurrentCulture);
// test for small letter string1 value .
bool result_b = str1.StartsWith("geek",
StringComparison.CurrentCulture);
// test for string2 value .
bool result_tt = str2.StartsWith("CSharp",
StringComparison.CurrentCulture);
bool result_t = str2.StartsWith("Learn",
StringComparison.CurrentCulture);
// Display result
Console.WriteLine(result_a);
Console.WriteLine(result_b);
Console.WriteLine(result_tt);
Console.WriteLine(result_t);
}
}
- 输出:
https://www.geeksforgeeks.org
- 程式2:
夏普
// C# program to illustrate the
// String.StartsWith (String) Method
using System;
class Geeks {
// Main Method
public static void Main()
{
string[] input_str = {
"
GeekforGeeks Computer Science Portal
",
"GeekforGeeks Sudo Placement
",
"GeekforGeeks Placement Preparation
",
"GeekforGeeks Contribute
",
"GeekforGeeks Contribute ",
"GeekforGeeks Interview
",};
// Display result after implementation StartsWith()
// method in strings starting to be tags removed.
foreach(var n in input_str)
Console.WriteLine(htmlStartTags(n));
}
private static string htmlStartTags(string str)
{
// To check starting "<"tag is or not
if (str.Trim().StartsWith("<"))
{
// Find the closing ">" tag.
int end = str.IndexOf(">");
// After getting tag, then remove the tag
if (end >= 0)
{
str = str.Substring(end + 1);
// Additional starting tags to be romove
str = htmlStartTags(str);
}
}
return str;
}
}
- 输出:
GeekforGeeks Computer Science Portal
GeekforGeeks Sudo Placement
GeekforGeeks Placement Preparation
GeekforGeeks Contribute
GeekforGeeks Contribute
GeekforGeeks Interview
笔记:
- 如果input_string为Null,则此方法将提供ArgumentNullException 。
- 此方法还通过使用当前区域性执行区分大小写和区域性区分的比较。
String.StartsWith(String,Boolean,CultureInfo)方法
此方法用于检查当前字符串实例的开头与使用指定区域性进行比较时是否与指定字符串匹配。如果找到匹配项,则返回字符串,否则返回false。
句法:
public bool StartsWith(string str,
bool case,
CultureInfo cul)
参数:
str: It is the string which is to be compared and the type of this parameter is System.String.
case: It will set true to ignore case during the comparison, otherwise false and the type of this parameter is System.Boolean.
cul: It is the Cultural information which checks how current string and str are compared. If culture is null, the current culture is used and the type of this parameter is System.Globalization.CultureInfo.
返回值:该函数返回System.Boolean类型的值,如果str与当前字符串的开头匹配,则返回true ,否则返回false 。
异常:如果str的值为null,则此方法将提供ArgumentNullException 。
例子:
夏普
// C# program to illustrate the
// String.StartsWith (string,
// bool, CultureInfo) Method
using System.Threading;
using System.Globalization;
using System;
class Sudo {
// Main Method
public static void Main(string[] args)
{
// Input string
string str1 = "Geeks";
// Implementaion of startswith() function
// test in original string.
bool result_a = str1.StartsWith("Geeks", false,
CultureInfo.InvariantCulture);
// test in small letter string.
bool result_b = str1.StartsWith("geeks", false,
CultureInfo.InvariantCulture);
// test in capital letter string.
bool result_c = str1.StartsWith("GEEKS", false,
CultureInfo.InvariantCulture);
// test in no string parameter .
bool result_d = str1.StartsWith(" ", false,
CultureInfo.InvariantCulture);
// Display result
Console.WriteLine(result_a);
Console.WriteLine(result_b);
Console.WriteLine(result_c);
Console.WriteLine(result_d);
}
}
输出:
True
False
False
False
String.StartsWith(String,StringComparison)方法
使用指定的比较选项进行比较时,此方法用于检查当前字符串实例的开头是否与指定的字符串匹配。如果找到匹配项,则返回字符串,否则返回false。
句法:
bool StartsWith(String str, StringComparison cType)
参数:
str: It is the required string which is to be compared and type of this parameter is System.String.
cType: It is one of the enumeration values that determine how current string and str are compared. Type of this parameter is System.StringComparison.
返回值:该函数返回布尔值,即true,如果它发现一个匹配,否则将返回false。返回类型为System.Boolean 。
例外情况:
- 如果str的值为null,则此方法将提供ArgumentNullException 。
- 如果cType的值不是StringComparison值,则此方法将提供ArgumentException 。
例子:
夏普
// C# program to illustrate the
// StartsWith(String, StringComparison)
// method
using System;
class Sudo {
// Main Method
public static void Main(string[] args)
{
// Input two string
string str1 = "GeeksforGeeks";
string str2 = "Learn CSharp";
// Implementaion of startswith() function
// test for original string1 value.
bool result_a = str1.StartsWith("Geek",
StringComparison.CurrentCulture);
// test for small letter string1 value .
bool result_b = str1.StartsWith("geek",
StringComparison.CurrentCulture);
// test for string2 value .
bool result_tt = str2.StartsWith("CSharp",
StringComparison.CurrentCulture);
bool result_t = str2.StartsWith("Learn",
StringComparison.CurrentCulture);
// Display result
Console.WriteLine(result_a);
Console.WriteLine(result_b);
Console.WriteLine(result_tt);
Console.WriteLine(result_t);
}
}
输出:
True
False
False
True
参考:
- https://msdn.microsoft.com/zh-CN/library/baketfxw(v=vs.110).aspx
- https://msdn.microsoft.com/zh-CN/library/6k0axhx9(v=vs.110).aspx
- https://msdn.microsoft.com/zh-CN/library/ms131452(v=vs.110).aspx