C# 检查字符串字符是否与100%字符数组匹配?
有没有一种好方法可以检查字符串是否包含100%字符数组,相同的字符可以多次出现 字符数组:C# 检查字符串字符是否与100%字符数组匹配?,c#,C#,有没有一种好方法可以检查字符串是否包含100%字符数组,相同的字符可以多次出现 字符数组: private static char[] Letter = { 'd', 'o' , 'g' }; 因此,它将匹配: string a = "dog" string b = "dooooggg" 但不匹配` string c = "doig" 如果想说得更具体些,我能描述一下字符串中每个字符可以匹配多少个吗?使用Linq char[] Letter = { 'd', 'o', 'g' }; str
private static char[] Letter = { 'd', 'o' , 'g' };
因此,它将匹配:
string a = "dog"
string b = "dooooggg"
但不匹配`
string c = "doig"
如果想说得更具体些,我能描述一下字符串中每个字符可以匹配多少个吗?使用
Linq
char[] Letter = { 'd', 'o', 'g' };
string b = "dooooggg";
bool result = b.All(Letter.Contains);
使用
Linq接近
char[] Letter = { 'd', 'o', 'g' };
string b = "dooooggg";
bool result = b.All(Letter.Contains);
您可以使用Intersect
、Take
和Count
,根据需要支持最小匹配计数:
char[] Letter = { 'd', 'o', 'g' };
int minMatchCount = 2;
string a = "dog"; // 3 matches, fine
string b = "dooooggg"; // 3 matches, fine
string c = "dock"; // 2 matches, fine
string d = "foo"; // nope, only 1
bool minMatchingA = a.Intersect(Letter).Take(minMatchCount).Count() == minMatchCount; // true
bool minMatchingB = b.Intersect(Letter).Take(minMatchCount).Count() == minMatchCount; // true
bool minMatchingC = c.Intersect(Letter).Take(minMatchCount).Count() == minMatchCount; // true
bool minMatchingD = d.Intersect(Letter).Take(minMatchCount).Count() == minMatchCount; // false
如果您想知道是否包含所有字符,请将minMatchCount
设置为Letter.Length您可以使用Intersect
、Take
和Count
,根据需要支持最小匹配计数:
char[] Letter = { 'd', 'o', 'g' };
int minMatchCount = 2;
string a = "dog"; // 3 matches, fine
string b = "dooooggg"; // 3 matches, fine
string c = "dock"; // 2 matches, fine
string d = "foo"; // nope, only 1
bool minMatchingA = a.Intersect(Letter).Take(minMatchCount).Count() == minMatchCount; // true
bool minMatchingB = b.Intersect(Letter).Take(minMatchCount).Count() == minMatchCount; // true
bool minMatchingC = c.Intersect(Letter).Take(minMatchCount).Count() == minMatchCount; // true
bool minMatchingD = d.Intersect(Letter).Take(minMatchCount).Count() == minMatchCount; // false
如果您想知道是否包含所有字符,请将minMatchCount
设置为Letter.Length
顺序是否重要?i、 “上帝”匹配吗?上帝也应该匹配,谢谢。顺序重要吗?i、 “上帝”匹配吗?上帝也应该匹配,谢谢。谢谢,我会测试它。谢谢,我会测试它。