C# 比较字符串
如果字符串a喜欢字符串b形式,是否有任何方法允许我们返回true 考试:C# 比较字符串,c#,string,C#,String,如果字符串a喜欢字符串b形式,是否有任何方法允许我们返回true 考试: "12:2".Like("*:*") = true 或 谢谢 您可以使用正则表达式使用以下函数 Regex.IsMatch("string", "your expression") 以下行的实例应返回true: Regex.IsMatch("12:2", "/[0-9]{2,}:[0-9]{1,}/") 注意:这样,您每次都必须为不同的格式创建exp 您可以使用以下方法检查给定字符串是否与带通配符的类DOS模式
"12:2".Like("*:*") = true
或
谢谢 您可以使用正则表达式使用以下函数
Regex.IsMatch("string", "your expression")
以下行的实例应返回true
:
Regex.IsMatch("12:2", "/[0-9]{2,}:[0-9]{1,}/")
注意:这样,您每次都必须为不同的格式创建exp
您可以使用以下方法检查给定字符串是否与带通配符的类DOS模式匹配(即,用“?”表示一个字符,用“*”表示零个或多个字符的模式): 你可以这样称呼它:
bool match = IsMatch("what is your name?", "*is*name*?"); // Returns true
您可以使用以下未优化的方法。该函数可能没有考虑某些情况,但我认为它为您提供了一个起点 另一种可能的解决方案是使用正则表达式
public static bool Like(string pattern, string str)
{
string[] words = pattern.Split('*').Where(w => w.Trim() != string.Empty).ToArray();
List<int> indeces = new List<int>();
for (int i = 0, l = words.Length; i < l; i++)
{
int wordIndex = str.IndexOf(words[i], StringComparison.OrdinalIgnoreCase);
if (wordIndex == -1)
return false;
else
indeces.Add(wordIndex);
}
List<int> sortedIndeces = indeces.ToList();
sortedIndeces.Sort();
for (int i = 0, l = sortedIndeces.Count; i < l; i++)
{
if (sortedIndeces[i] != indeces[i]) return false;
}
return true;
}
publicstaticbool-Like(stringpattern,stringstr)
{
string[]words=pattern.Split('*')。其中(w=>w.Trim()!=string.Empty)。ToArray();
列表索引=新列表();
for(int i=0,l=words.Length;i
祝您好运不,但您可以查看常规表达式regex是您需要的是“*”您唯一的运算符吗?使用regex或使用VB.NET,它可能与
bool match = IsMatch("what is your name?", "*is*name*?"); // Returns true
public static bool Like(string pattern, string str)
{
string[] words = pattern.Split('*').Where(w => w.Trim() != string.Empty).ToArray();
List<int> indeces = new List<int>();
for (int i = 0, l = words.Length; i < l; i++)
{
int wordIndex = str.IndexOf(words[i], StringComparison.OrdinalIgnoreCase);
if (wordIndex == -1)
return false;
else
indeces.Add(wordIndex);
}
List<int> sortedIndeces = indeces.ToList();
sortedIndeces.Sort();
for (int i = 0, l = sortedIndeces.Count; i < l; i++)
{
if (sortedIndeces[i] != indeces[i]) return false;
}
return true;
}