相关疑难解决方法(0)

C#中字符串比较方法的差异

在C#中比较字符串非常简单.事实上,有几种方法可以做到这一点.我在下面的块中列出了一些.我很好奇的是它们之间的差异以及何时应该使用其他的?是否应该不惜一切代价避免?还有更多我没有列出?

string testString = "Test";
string anotherString = "Another";

if (testString.CompareTo(anotherString) == 0) {}
if (testString.Equals(anotherString)) {}
if (testString == anotherString) {}
Run Code Online (Sandbox Code Playgroud)

(注意:我在这个例子中寻找平等,不小于或大于,但也可以随意发表评论)

c# string comparison

256
推荐指数
7
解决办法
19万
查看次数

C#String Comparison等于false

我有一个字符串比较问题 - 在大多数情况下 - 表现得如预期的那样,但是由于我的代码没有将字符串对检测为重复,因此留下了大量重复数据库插入.

我以为我把它缩小到一个文化问题(西里尔字符),我解决了,但我现在得到'假阴性'(两个显然相等的字符串显示为不相等).

我查看了以下类似的问题,并尝试了以下比较方法.

我检查的类似SO问题:

以下是比较字符串的示例:(标题和说明)

饲料标题:埃尔斯伯格:他是英雄

饲料设计:丹尼尔埃尔斯伯格告诉美国有线电视新闻网的唐柠檬,国家安全局的骗子爱德华斯诺登表现出了勇气,做了大量的服务.

db title: Ellsberg:他是英雄

db desc:丹尼尔埃尔斯伯格告诉美国有线电视新闻网的唐柠檬,国家安全局的骗子爱德华斯诺登表现出勇气,做了大量的服务.

我的应用程序将从RSS提要获取的值与我在数据库中提供的值进行比较,并且只应插入"新"值.

//fetch existing articles from DB for the current feed:
    List<Article> thisFeedArticles = (from ar in entities.Items
                                      where (ar.ItemTypeId == (int)Enums.ItemType.Article) && ar.ParentId == feed.FeedId
                                      && ar.DatePublished > datelimit
                                      select new Article
                                      {
                                           Title = ar.Title, 
                                           Description = ar.Blurb
                                      }).ToList();
Run Code Online (Sandbox Code Playgroud)

以下比较的每个人都显示与Ellsberg标题/描述不匹配.ie matches1 to matches6都有Count()==0

(请原谅列举的变量名称 - 它们仅用于测试)

   // comparison methods 
CompareOptions compareOptions = CompareOptions.OrdinalIgnoreCase;
CompareOptions compareOptions2 …
Run Code Online (Sandbox Code Playgroud)

.net c# string-comparison

7
推荐指数
1
解决办法
2001
查看次数

标签 统计

c# ×2

.net ×1

comparison ×1

string ×1

string-comparison ×1