不区分大小写的“包含(字符串)" [英] Case insensitive 'Contains(string)'

查看:41
本文介绍了不区分大小写的“包含(字符串)"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法让下面的结果为真?

Is there a way to make the following return true?

string title = "ASTRINGTOTEST";
title.Contains("string");

似乎没有允许我设置区分大小写的重载..目前我把它们都大写,但这只是愚蠢的(我指的是 i18n 上下大小写问题).

There doesn't seem to be an overload that allows me to set the case sensitivity.. Currently I UPPERCASE them both, but that's just silly (by which I am referring to the i18n issues that come with up- and down casing).

更新
这个问题很古老,从那时起我就意识到,如果您愿意对一个非常庞大而困难的话题进行全面调查,我会要求一个简单的答案.
在大多数情况下,在单语、英语代码库中这个答案就足够了.我怀疑是因为大多数来这里的人都属于这一类,这是最受欢迎的答案.
这个答案提出了一个固有问题,即在我们知道两个文本是相同的文化之前,我们无法比较不区分大小写的文本我们知道这种文化是什么.这可能是一个不太受欢迎的答案,但我认为它更正确,这就是我将其标记为这样的原因.

UPDATE
This question is ancient and since then I have realized I asked for a simple answer for a really vast and difficult topic if you care to investigate it fully.
For most cases, in mono-lingual, English code bases this answer will suffice. I'm suspecting because most people coming here fall in this category this is the most popular answer.
This answer however brings up the inherent problem that we can't compare text case insensitive until we know both texts are the same culture and we know what that culture is. This is maybe a less popular answer, but I think it is more correct and that's why I marked it as such.

推荐答案

测试字符串paragraph是否包含字符串word(感谢@QuarterMeister)

To test if the string paragraph contains the string word (thanks @QuarterMeister)

culture.CompareInfo.IndexOf(paragraph, word, CompareOptions.IgnoreCase) >= 0

其中 cultureCultureInfo 描述文本所用的语言.

Where culture is the instance of CultureInfo describing the language that the text is written in.

此解决方案对于不区分大小写的定义是透明的,这取决于语言.例如,英语使用字符 Ii 表示第九个字母的大小写版本,而土耳其语使用这些字符表示 其 29 个字母长的字母表中的第十一个和第十二个字母.'i' 的土耳其语大写版本是不熟悉的字符 'İ'.

This solution is transparent about the definition of case-insensitivity, which is language dependent. For example, the English language uses the characters I and i for the upper and lower case versions of the ninth letter, whereas the Turkish language uses these characters for the eleventh and twelfth letters of its 29 letter-long alphabet. The Turkish upper case version of 'i' is the unfamiliar character 'İ'.

因此字符串tinTIN 在英语中是同一个词,但在土耳其语中是不同的词.据我了解,一个是精神"的意思,另一个是象声词.(土耳其人,如果我错了,请纠正我,或者提出一个更好的例子)

Thus the strings tin and TIN are the same word in English, but different words in Turkish. As I understand, one means 'spirit' and the other is an onomatopoeia word. (Turks, please correct me if I'm wrong, or suggest a better example)

总而言之,您只能回答这两个字符串是否相同但在不同的情况下"这个问题如果您知道文本使用的是哪种语言.如果你不知道,你将不得不采取平底船.鉴于英语在软件方面的霸权,您可能应该求助于CultureInfo.InvariantCulture,因为它会以熟悉的方式出错.

To summarise, you can only answer the question 'are these two strings the same but in different cases' if you know what language the text is in. If you don't know, you'll have to take a punt. Given English's hegemony in software, you should probably resort to CultureInfo.InvariantCulture, because it will be wrong in familiar ways.

这篇关于不区分大小写的“包含(字符串)"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆