我曾经需要使用StartsWith方法做类似的事情。这是一个简单的解决方案,参考自@Serge - appTranslator。
以下是一个扩展方法:
public static bool StartsWith(this string str, string value, CultureInfo culture, CompareOptions options)
{
if (str.Length >= value.Length)
return string.Compare(str.Substring(0, value.Length), value, culture, options) == 0;
else
return false;
}
对于一行代码迷 ;)
public static bool StartsWith(this string str, string value, CultureInfo culture, CompareOptions options)
{
return str.Length >= value.Length && string.Compare(str.Substring(0, value.Length), value, culture, options) == 0;
}
不区分重音和大小写的startsWith函数可以这样调用:
value.ToString().StartsWith(str, CultureInfo.InvariantCulture, CompareOptions.IgnoreNonSpace | CompareOptions.IgnoreCase)
string.Normalize
方法? - Andre Soares