tags:

views:

43

answers:

2

Is there any solution to overcome case-sensitive problem for contains method.

I have code like below

string str = m_name;
return avobj.Viewname.Contains(str);

Eg: Welcome Here welcome here

Both are same names but case is different. If I give 'W' in search box it is returning only 1st one. but I need both names display.

I am storing the names in collection. And resultant values ( searched values ) are storing in List.

+4  A: 

You can use String.IndexOf(string, StringComparison). If it returns anything other than -1, then the substring was present. You can then specify an appropriately case-insensitive comparison.

Jon Skeet
could you give me more details??..i dint understand properly
Srikanth
@Srikanth - the code I found implements just that.
Daniel A. White
@Srikanth: `return avobj.Viewname.IndexOf(str, StringComparison.CurrentCultreIgnoreCase) != -1`
Jon Skeet
Thanx alot....one more doubt...I am dynamically adding a string to the name in collection. Now the problem is it is searching the dynamic string also..I dont want to search it..how to avoid ?...Dynamic string is as : [Default]
Srikanth
@Srikanth: It's not really obvious what you're doing, but can't you check for containment *before* you add the extra value?
Jon Skeet
I have to add that string. There will be no change in that. any solution for above scenario??
Srikanth
@Srikanth: But why can't you add it *after* you've checked for the string you're searching for? Alternatively you *could* just ignore any matching string which is also the "default" value - but that would cause problems if that value was ever a valid one. To be honest it's not clear enough what you're doing to make very helpful suggestions.
Jon Skeet
@Jon: The code which u gave is working for 'space' also. i.e if i give 'space' in search text box it is displaying the names which have spaces in between words.
Srikanth
That should not happen..plz suggest.
Srikanth
@Srikanth: Trying to redesign your software in SO comments isn't really the best way to go. Work out what behaviour you want in various scenarios, do your best to implement it yourself, and then ask a new question with the new requirements - as well as how far you've already got.
Jon Skeet
+1  A: 
public static bool ContainsCaseInsensitive(this string source, string value)
{
  int results = source.IndexOf(value, StringComparison.CurrentCultureIgnoreCase);
  return results != -1;
}

Source: http://schleichermann.wordpress.com/2009/02/24/c-stringcontains-case-insensitive-extension-method/

Daniel A. White
The return statement is more simply written as: `return results != -1;`
Jon Skeet