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).
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.
You can use
IndexOf()
like this:Since 0 (zero) can be an index, you check against -1.
MSDN
OrdinalIgnoreCase, CurrentCultureIgnoreCase or InvariantCultureIgnoreCase?
Since this is missing, here are some recommendations about when to use which one:
Dos
StringComparison.OrdinalIgnoreCase
for comparisons as your safe default for culture-agnostic string matching.StringComparison.OrdinalIgnoreCase
comparisons for increased speed.StringComparison.CurrentCulture-based
string operations when displaying the output to the user.StringComparison.Ordinal
orStringComparison.OrdinalIgnoreCase
when the comparison islinguistically irrelevant (symbolic, for example).
ToUpperInvariant
rather thanToLowerInvariant
when normalizing strings for comparison.Don'ts
StringComparison.InvariantCulture
-based stringoperations in most cases; one of the few exceptions would be
persisting linguistically meaningful but culturally-agnostic data.
Based on these rules you should use:
whereas [YourDecision] depends on the recommendations from above.
link of source: http://msdn.microsoft.com/en-us/library/ms973919.aspx
The trick here is to look for the string, ignoring case, but to keep it exactly the same (with the same case).
Output is "Reset"
These are the easiest solutions.
By Index of
By Changing case
By Regex
This is quite similar to other example here, but I've decided to simplify enum to bool, primary because other alternatives are normally not needed. Here is my example:
And usage is something like:
This is clean and simple.