我希望将两个字符串相互匹配,同时注意三个条件:
1个案例的敏感性(应该都不区分大小写):wHo <=>谁
2-underscore:father_of <=>父亲
3个缺失的空间:barackobama <=> barack obama
因此,以下两个字符串应该相互匹配
谁是barack_obama的父亲<=>谁是巴拉克奥巴马的父亲
我不知道从哪里开始,我试图得到两个字符串的排列,考虑下划线和缺少空格的情况,所以它会像
Who, is, fatherof, barack_obama
who is, is fatherof, fatherof barack_obama,
whois, isfatherof, fatherofbarack_obama,
who_is, is_fatherof, fatherof_barack_obama,
who is fatherof, is fatherof barack_obama
whoisfatherof, isfatherofbarack_obama
who_is_fatherof, is_fatherof_barack_obama
who is fatherof barack_obama
whoisfatherofbarack_obama
who_is_fatherof_barack_obama
Run Code Online (Sandbox Code Playgroud)
这对于将barack obama与barack_obama匹配有好处,但反之亦然,即使我能够在其间拆分带有undserscore的字符串,我也不能用缺少的空间来做
也许做:
public static class StringExtensions
{
private string NormalizeText(string text)
{
return text..Replace("_","")
.Replace(" ","")
.Replace(",","");
}
public static bool CustomEquals(this string instance, string otherString)
{
return NormalizeText(instance).Equals(NormalizeText(otherString),
StringComparison.CurrentCultureIgnoreCase);
}
}
Run Code Online (Sandbox Code Playgroud)
所以
"Who is the fatherof barack_obama"
"who IS the father of barack obama"
Run Code Online (Sandbox Code Playgroud)
比较(忽略情况)
"Whoisthefatherofbarackobama"
"whoISthefatherofbarackobama"
Run Code Online (Sandbox Code Playgroud)