- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在我的应用程序的一部分中,我有一个选项可以显示当前艺术家不在音乐库中的专辑列表。为此,我调用音乐 API 来获取该艺术家的所有专辑列表,然后删除当前库中的专辑。
为了应对名称的不同大小写以及标题中丢失(或额外标点符号)的可能性,我写了一个 IEqualityComparer
用于 .Except
称呼:
var missingAlbums = allAbumns.Except(ownedAlbums, new NameComparer());
这是
Equals
方法:
public bool Equals(string x, string y)
{
// Check whether the compared objects reference the same data.
if (ReferenceEquals(x, y)) return true;
// Check whether any of the compared objects is null.
if (x is null || y is null)
return false;
return string.Compare(x, y, CultureInfo.CurrentCulture, CompareOptions.IgnoreCase | CompareOptions.IgnoreSymbols) == 0;
}
这是
GetHashCode
方法:
public int GetHashCode(string obj)
{
// Check whether the object is null
if (obj is null) return 0;
// Make lower case. How do I strip symbols?
return obj.ToLower().GetHashCode();
}
当然,当字符串包含符号时,这会失败,因为我在获取哈希码之前没有删除它们,因此两个字符串(例如“Baa,baa,blacksheep”和“Baa baa Blacksheep”)仍然不相等,甚至转换为小写后。
CompareOptions.IgnoreSymbols
存在,有没有我可以调用的方法可以从字符串中去除这些字符?或者失败了,一个将返回所有符号的方法?
IsPunctuation
字符的方法,但我无法确定它认为是标点符号的内容是否与字符串比较选项认为是符号的内容相同。
最佳答案
如果您打算使用 CompareOptions
枚举,我觉得你不妨将它与 CompareInfo
一起使用它被记录为专为以下目的设计的类:
Defines the string comparison options to use with CompareInfo.
GetHashCode(string, CompareOptions)
来自该类的方法(如果您愿意,甚至可以使用
Compare(string, string, CompareOptions)
方法)。
关于c# - 我需要从字符串中去除所有符号以创建一个忽略标点符号的 `IEqualityComparer`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67994184/
'Equal' 和 'GetHashcode' 方法存在于对象类中,我们的类型继承了对象基类。 直接实现对象的两种方法和使用 IComparer 接口(interface)有什么区别? 如果我们覆盖对
在 .NET 中,IEnumerable延伸IEnumerable经常派上用场。令人沮丧的是IEqualityComparer和 IComparer不要扩展它们的非通用对应物,尽管 EqualityC
在我的应用程序的一部分中,我有一个选项可以显示当前艺术家不在音乐库中的专辑列表。为此,我调用音乐 API 来获取该艺术家的所有专辑列表,然后删除当前库中的专辑。 为了应对名称的不同大小写以及标题中丢失
假设我像这样实例化了一个字典 var dictionary = new Dictionary(); MyClass 是我自己的类,它实现了一个 IEqualityComparer<> . 现在,当我
我想为 Nullable 结构编写一个相等比较器。比方说,DateTime? .所以我想出了这段代码: public class NullableEntityComparer : IEqualityC
这是我的测试代码(包含没有在应该返回 true 的时候返回 true): HashSet test = new HashSet(new TilePositionCompare()); test.Add
我有一个不可变的值对象 IPathwayModule,其值定义为: (int) block ; (实体)模块,由(字符串)ModuleId 标识; (枚举)状态;和 (实体)类,由(字符串)Class
我有一个 List存储在我的计算机上的文件路径。我的目的是先过滤掉同名的文件,再过滤掉大小相同的文件。 为此,我创建了两个类来实现 IEqualityComparer , 并实现 Equals和 Ge
我只是想从两个列表中删除重复项并将它们合并到一个列表中。我还需要能够定义什么是重复项。我通过 ColumnIndex 属性定义了一个副本,如果它们相同,则它们是重复的。这是我采用的方法: 我找到了一个
我正在尝试设置一个使用字符串列表作为比较属性的 IEqualityComparer。 在下面的 2 行代码中使用 Except 和 Intersect 时,所有记录都被视为"new",没有一个被识别为
给定两个类 class Contract { public int ID {get; set;} // . . . } class DBContract { public in
我想知道是否有可能在 Distinct 中使用单例作为比较器对象?? 假设我有一个元素列表,我需要在该列表上使用不同的函数。通常我会这样做 var result = list.Distinct(ne
首先我看到IEqualityComparer for anonymous type那里的答案没有回答我的问题,很明显我需要一个 IEqualityComparer不和IComparer用于 Linq
我想从列表中获取不同的对象。我尝试实现 IEqualityComparer 但没有成功。请查看我的代码并给我一个关于 IEqualityComparer 的解释。 public class Messa
我的数据库中有一些相同编号的铃铛。我想得到所有这些而不重复。我创建了一个比较类来完成这项工作,但是该函数的执行导致没有 distinct 的函数出现很大的延迟,从 0.6 秒到 3.2 秒! 我做得对
我正在做一些需要自定义比较器的 LINQ,所以我创建了一个实现 IEqualityComparer 的新类.但是,当我使用它时,我每次都必须创建它的一个实例。 Dim oldListOnly = ol
IEqualityComparer 是否公开,其行为与相等 (=) 运算符相同? LanguagePrimitives 模块包含几个:FastGenericEqualityComparer、Gener
我正在努力实现以下目标: 我有一个表单,用户必须输入姓名、姓氏、地址……以及保存更改按钮。 我想让用户点击保存更改 仅当他确实进行了任何更改时才按下按钮。我发现通过简单地使用 IChangeTrack
我有一个自定义对象列表,我试图在其中删除重复记录。我看到很多在线文章都指向 IEqualityComparer(我以前从未使用过它)。 问题是,我应该什么时候使用它?我可以通过 LINQ 在一行代码中
我想要一个 IEqualityComparer当且仅当两个泛型类型相同且忽略泛型参数时返回 true。所以comparer.Equals(typeof(List), typeof(List))应该返回
我是一名优秀的程序员,十分优秀!