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

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

问题描述

有没有一种方法,使以下返回正确的?

Is there a way to make the following return true?

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

似乎没有成为过载,使我设置区分大小写。目前我大写他们两个,不过这只是愚蠢的。

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.

更新结果
我指的是sillyness是国际化附带上和下套管问题。

UPDATE
The sillyness I refer to is the i18n issues that come with up- and down casing.

更新结果
这个问题是一个古老而从那以后,我已经意识到我问一个简单的答案一个真正庞大而棘手的问题,如果你愿意全面调查。结果
在大多数情况下,在单语种,英语code碱基答案就足够了。我怀疑,因为大多数人来这里属于这一类,这是最流行的答案。照片
回答然而带来了,我们不能比较文本不区分大小写的,直到我们知道这两种文本具有同样的文化固有的问题而我们知道,文化是什么。这也许是不太受欢迎的答案,但我认为这是比较正确的,这就是为什么我将其标记为此类。

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.

推荐答案

要测试是否字符串段落包含字符串(感谢@QuarterMeister)

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

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

其中,文化是<一个实例href=\"http://msdn.microsoft.com/en-gb/library/system.globalization.cultureinfo(v=vs.110).aspx\"><$c$c>CultureInfo描述该文本用的语言。

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

此解决方案是公开透明的不区分大小写的定义,这是依赖于语言即可。例如,英语语言使用字符 I I 的第九个字母的大写和小写版本,而土耳其的语言使用这些字符的第十一和第十二字母其29日的信长的字母。 我在土耳其大写的版本是不熟悉的'我'。

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 'İ'.

因此​​,弦是同一个字的英文的,但不同的字土耳其的。据我所知,一是指精神,另一个是一个象声词的单词。 (土耳其人,请纠正我,如果我错了,或提出一个更好的例子)

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'll be wrong in familiar ways.

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

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