From: mp on 23 Sep 2009 12:02 "Peter Duniho" <no.peted.spam(a)no.nwlink.spam.com> wrote in message news:op.u0o27aapvmc1hu(a)macbook-pro.local... > On Tue, 22 Sep 2009 13:34:03 -0700, mp <nospam(a)thanks.com> wrote: > >> Is there a way to make String.Contains do a case insensitive comparison? >> like an equivalent of vb6 Option CompareText in a code module? > > It is odd that Contains() doesn't include an overload that takes a > StringComparison value. It's so odd, we only a month ago had this same > discussion: > http://groups.google.com/group/microsoft.public.dotnet.languages.csharp/browse_thread/thread/ebc4258ed2c4270f/c48b6c15df01e8bf > snip Thanks for the link, i'll check it out mark
From: Peter Duniho on 23 Sep 2009 12:48 On Wed, 23 Sep 2009 00:52:01 -0700, Morten Wennevik [C# MVP] <MortenWennevik(a)hotmail.com> wrote: > I disagree. You can disagree all you like. It doesn't undo the documented problem. It is true that if you can limit the input to ASCII, or some particular culture, it is possible to get away with using case-conversion. But .NET operates on Unicode strings, and provides no concise way to represent those limitations. Furthermore, when you are posting to a public newsgroup, you have no way to guarantee that code that uses case-conversion will be used in this limited way even by convention. It is nearly as easy to do the comparison _correctly_ as it is to do it incorrectly. It makes no sense to prefer to write the code to do it incorrectly. Pete
From: not_a_commie on 23 Sep 2009 12:53 Use the string.Compare or string.Equals static functions.
From: Harlan Messinger on 23 Sep 2009 15:23 Peter Duniho wrote: > On Wed, 23 Sep 2009 00:52:01 -0700, Morten Wennevik [C# MVP] > <MortenWennevik(a)hotmail.com> wrote: > >> I disagree. > > You can disagree all you like. It doesn't undo the documented problem. It's the same problem no matter how you factor it. In the other thread you wrote that "It's the fact that no matter what the culture, simply changing the case of the string and comparing will not necessarily produce the same results as doing an actual "case insensitive" comparison for that culture." I don't see that a "case insensitive comparison" between a target character and a character that has been read in is different from comparing the target character to both the read-in character and its alternate-case counterpart. If you've got input that's permitted to be fuzzy on case, allowing "X" as well as "x" and "N" instead of "n", then your code is going to have to be able to figure out that "PORTRAIT" and "pOrTrAiT" and "porTRaIT" are all the same as "portrait". Whatever comparison you do is going to incorporate some assumption as to which characters correspond in an upper- and lower-case relationship, whether you have s1.ToLower().Contains(s2.ToLower()) or you use a case-insensitive regular expression or you hard-code a letter-by-letter comparison. You can't handle every case: your Turk could type "portrait" or "portraıt" or "PORTRAIT" or "PORTRAİT" into the configuration file. So you have to make an assumption, and make it explicit what that assumption is, and then feel free to code against what that assumption is and treat anything else as an error condition. Or you can specify that configuration data and user input are going to be treated as case sensitive and then not worry about case at all. > It is true that if you can limit the input to ASCII, or some particular > culture, it is possible to get away with using case-conversion. But > .NET operates on Unicode strings, and provides no concise way to > represent those limitations. Furthermore, when you are posting to a > public newsgroup, you have no way to guarantee that code that uses > case-conversion will be used in this limited way even by convention. > > It is nearly as easy to do the comparison _correctly_ as it is to do it > incorrectly. It makes no sense to prefer to write the code to do it > incorrectly.
From: Sreenivas on 24 Sep 2009 00:27 On Sep 23, 10:38 am, "Peter Duniho" <no.peted.s...(a)no.nwlink.spam.com> wrote: > On Tue, 22 Sep 2009 22:15:46 -0700, Sreenivas > > <thatiparthysreeni...(a)gmail.com> wrote: > > I think using RegularExpressions are overkill ,in this case . > > do in this way, > > if(line.ToLower().Contains()) > > { > > listBox1.Items.Add(line); > > } > > Regex might be a little bit of overkill as compared to the most efficient > alternatives, but simply changing the case is a much poorer solution. > Please refer to the previous discussion (I provided a link in my other > reply to the OP) for details on why. > > I would definitely use Regex before I would be willing to simply convert > the case. > Pete Pete , i have a question. Does RegexOptions.IgnoreCase( in the code snippet i posted ) guarantee the comparison of all Unicode characters? What i am asking is , is my code snippet works for all unicode characters? Thanks, Sreenivas Reddy Thatiparthy.
First
|
Prev
|
Next
|
Last
Pages: 1 2 3 4 Prev: How to determine if /3GB is active. Next: Problem with BinaryFormatter Deserialize method |