标签云

微信群

扫码加入我们

WeChat QR Code

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 (by which I am referring to the i18n issues that come with up- and down casing).UPDATEThis 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 knowwhat 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.


Why not culture.CompareInfo.IndexOf(paragraph, word, CompareOptions.IgnoreCase) >= 0?That uses the right culture and is case-insensitive, it doesn't allocate temporary lowercase strings, and it avoids the question of whether converting to lowercase and comparing is always the same as a case-insensitive comparison.

2019年10月20日47分02秒

This solution also needlessly pollutes the heap by allocating memory for what should be a searching function

2019年10月20日47分02秒

Comparing with ToLower() will give different results from a case-insensitive IndexOf when two different letters have the same lowercase letter.For example, calling ToLower() on either U+0398 "Greek Capital Letter Theta" or U+03F4 "Greek Capital Letter Theta Symbol" results in U+03B8, "Greek Small Letter Theta", but the capital letters are considered different.Both solutions consider lowercase letters with the same capital letter different, such as U+0073 "Latin Small Letter S" and U+017F "Latin Small Letter Long S", so the IndexOf solution seems more consistent.

2019年10月20日47分02秒

Yes, you are very right about Turkish. ı => I, i => İ

2019年10月20日47分02秒

Why didn't you write "ddddfg".IndexOf("Df", StringComparison.OrdinalIgnoreCase) ?

2019年10月20日47分02秒

Great string extension method! I've edited mine to check the source string is not null to prevent any object reference errors from occurring when performing .IndexOf().

2019年10月20日47分02秒

This gives the same answer as paragraph.ToLower(culture).Contains(word.ToLower(culture)) with CultureInfo.InvariantCulture and it doesn't solve any localisation issues. Why over complicate things? stackoverflow.com/a/15464440/284795

2019年10月20日47分02秒

ColonelPanic the ToLower version includes 2 allocations which are unnecessary in a comparison / search operation.Why needlessly allocate in a scenario that doesn't require it?

2019年10月20日47分02秒

Seabiscuit that won't work because string is an IEnumerable<char> hence you can't use it to find substrings

2019年10月20日47分02秒

A word of warning: The default for string.IndexOf(string) is to use the current culture, while the default for string.Contains(string) is to use the ordinal comparer. As we know, the former can be changed be picking a longer overload, while the latter cannot be changed. A consequence of this inconsistency is the following code sample: Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;string self = "Waldstrasse"; string value = "straße";Console.WriteLine(self.Contains(value));/* False */ Console.WriteLine(self.IndexOf(value) >= 0);/* True */

2019年10月20日47分02秒

Good Idea, also we have a lot of bitwise combinations in RegexOptions like RegexOptions.IgnoreCase & RegexOptions.IgnorePatternWhitespace & RegexOptions.CultureInvariant; for anyone if helps.

2019年10月20日47分02秒

Must say I prefer this method although using IsMatch for neatness.

2019年10月21日47分02秒

What's worse, since the search string is interpreted as a regex, a number of punctuation chars will cause incorrect results (or trigger an exception due to an invalid expression).Try searching for "." in "This is a sample string that doesn't contain the search string".Or try searching for "(invalid", for that matter.

2019年10月21日47分02秒

cHao: In that case, Regex.Escape could help. Regex still seems unnecessary when IndexOf / extension Contains is simple (and arguably more clear).

2019年10月20日47分02秒

Note that I was not implying that this Regex solution was the best way to go. I was simply adding to the list of answers to the original posted question "Is there a way to make the following return true?".

2019年10月20日47分02秒

Search for "Turkey test" :)

2019年10月20日47分02秒

In some French locales, uppercase letters don't have the diacritics, so ToUpper() may not be any better than ToLower(). I'd say use the proper tools if they're available - case-insensitive compare.

2019年10月20日47分02秒

Don't use ToUpper or ToLower, and do what Jon Skeet said

2019年10月20日47分02秒

Just saw this again after two years and a new downvote... anyway, I agree that there are better ways to compare strings.However, not all programs will be localized (most won't) and many are internal or throwaway apps.Since I can hardly expect credit for advice best left for throwaway apps... I'm moving on :D

2019年10月21日47分02秒

Is searching for "Turkey test" the same as searching for "TURKEY TEST"?

2019年10月20日47分02秒

If toCheck is the empty string it needs to return true per the Contains documentation: "true if the value parameter occurs within this string, or if value is the empty string (""); otherwise, false."

2019年10月21日47分02秒

Based on amurra's comment above, doesn't the suggested code need to be corrected? And shouldn't this be added to the accepted answer, so that the best response is first?

2019年10月20日47分02秒

Now this will return true if source is an empty string or null no matter what toCheck is. That cannot be correct. Also IndexOf already returns true if toCheck is an empty string and source is not null. What is needed here is a check for null. I suggest if (source == null || value == null) return false;

2019年10月20日47分02秒

The source cant be null

2019年10月20日47分02秒

if (string.IsNullOrEmpty(source)) return string.IsNullOrEmpty(toCheck);

2019年10月20日47分02秒

This will match against a pattern, though. In your example, if fileNamestr has any special regex characters (e.g. *, +, ., etc.) then you will be in for quite a surprise. The only way to make this solution work like a proper Contains function is to escape fileNamestr by doing Regex.Escape(fileNamestr).

2019年10月20日47分02秒

why are you allowing ANOTHER layer of abstraction over StringComparison ?

2019年10月20日47分02秒

Finally ! It took a long time to be available...

2019年10月20日47分02秒

what if you know you're always gonna get an english string. which one to use?

2019年10月21日47分02秒

BKSpurgeon I'd use OrdinalIgnoreCase, if case does not matter

2019年10月20日47分02秒

Do you also know how it works internally?

2019年10月21日47分02秒

This is not culture-specific and may fail for some cases. culture.CompareInfo.IndexOf(paragraph, word, CompareOptions.IgnoreCase) should be used.

2019年10月20日47分02秒

Why avoid string.ToLower() when doing case-insensitive string comparisons? Tl;Dr It's costly because a new string is "manufactured".

2019年10月20日47分02秒

The questioner is looking for Contains not Compare.

2019年10月20日47分02秒

DuckMaestro, the accepted answer is implementing Contains with IndexOf. So this approach is equally helpful! The C# code example on this page is using string.Compare(). SharePoint team's choice that is!

2019年10月20日47分02秒

Your answer is exactly the same as guptat59's but, as was pointed out on his answer, this will match a regular expression, so if the string you're testing contains any special regex characters it will not yield the desired result.

2019年10月20日47分02秒

This is a straight up copy of this answer and suffers from the same issues as noted in that answer

2019年10月20日47分02秒

Downvote for the same reason Liam gave

2019年10月20日47分02秒

Agreed. Study regular expressions

2019年10月20日47分02秒

Assuming your paragraph and word will always be in en-US

2019年10月21日47分02秒

To avoid issues with forcing the culture to en-US, use return CultureInfo.CurrentCulture.CompareInfo.IndexOf(paragraph, word, CompareOptions.IgnoreCase) >= 0; instead.

2019年10月20日47分02秒

No this does not completely solve the problem.

2019年10月21日47分02秒

Downvote for just being incorrect. What if title = StRiNg? StRiNg != string and StRiNg != STRING

2019年10月20日47分02秒

I was wrong. Edit answer as follows, too simple simple:<br/>title.ToLower().Contains("string") // of course "string" islowercase

2019年10月20日47分02秒