小编典典

用通配符匹配字符串

c#

我想用通配符(*)匹配字符串,其中通配符表示“ any”。例如:

*X = string must end with X
X* = string must start with X
*X* = string must contain X

另外,一些复合用途例如:

*X*YZ* = string contains X and contains YZ
X*YZ*P = string starts with X, contains YZ and ends with P.

有一个简单的算法可以做到这一点吗?我不确定使用正则表达式(尽管有可能)。

为了清楚起见,用户将在上面的过滤器框中键入内容(尽可能简单的过滤器),我不希望他们自己编写正则表达式。因此,我可以轻松地从上述表示形式进行转换将是一件好事。


阅读 557

收藏
2020-05-19

共1个答案

小编典典

仅供参考,您 可以 使用VB.NET Like-Operator

string text = "x is not the same as X and yz not the same as YZ";
bool contains = LikeOperator.LikeString(text,"*X*YZ*", Microsoft.VisualBasic.CompareMethod.Binary);

使用CompareMethod.Text,如果你想忽略大小写。

您需要添加using Microsoft.VisualBasic.CompilerServices;

2020-05-19