假设我们有一个文本框和一个按钮。用户编写表格并单击按钮。
我希望我的程序接受任何以“ta”开头的文本。所以它会接受这个输入。有没有办法做到这一点?
要检查字符串是否以指定字符串开头,您可以使用StartsWith方法:
if(stringVariable.StartsWith("ta")) {
// starts with ta
}
让我们正确地做到这一点...... :)
/// <summary>
/// Determines whether the supplied string begins with "ta".
/// </summary>
/// <param name="value">The string to test.</param>
/// <returns>True if the supplied string starts with "ta"; false, otherwise.</returns>
/// <remarks>The comparison is case-insensitive.</remarks>
private static bool StartsWithTa(string value)
{
const string prefix = "ta";
if (value == null)
return false;
return value.StartsWith(prefix, StringComparison.CurrentCultureIgnoreCase);
}