Re: [ADVANCED-DOTNET] String.Compare seems to fail when it should not
At last, the problem is fixed.
One of the strings was found to be user input (that's a long story ...)
And users can insert any char they want to ...
We were put on the wrong track, since our db converts the error log
into ascii chars; what we read in the log was not equal to what had
happened at runtime! (OMG)
Thanks to anyone who replied.
// Ryan
On Fri, Sep 5, 2008 at 4:17 PM, Ryan Heath <[EMAIL PROTECTED]> wrote:
> Hi,
>
> See following snippet,
>
> if (0 != string.Compare(str1, str2, StringComparison.OrdinalIgnoreCase))
> {
> throw new Exception("Compare failed str1:({0}) length:{1} str2:({2})
> length:{3}", str1, str1.Length, str2, str2.Length);
> }
>
> it throws, for instance, with "Compare failed str1:(-mylife) length:7
> str2:(-MYLIFE) length:7"
>
> Obviously something is really wrong here...
> Has anyone seen this behavior before, or has anyone some tips how to
> investigate this problem?
>
> // Ryan
>
===
This list is hosted by DevelopMentor® http://www.develop.com
View archives and manage your subscription(s) at http://discuss.develop.com
Re: [ADVANCED-DOTNET] String.Compare seems to fail when it should not
For reference, that first one is me... On Tue, Sep 9, 2008 at 00:32, Sébastien Lorion <[EMAIL PROTECTED]> wrote: > I especially like the comments at the bottom ... That's funny and sad at the > same time :p > > Don't you DARE close this without fixing like you did with > TypeConverter.IsValid. > Posted by IDisposable1 on 11/16/2006 at 2:44 PM > > They dared > Posted by Marc Gravell on 11/27/2006 at 10:10 PM > > On Mon, Sep 8, 2008 at 10:03 PM, Mark Hurd <[EMAIL PROTECTED]> wrote: > >> On Tue, Sep 9, 2008 at 5:57 AM, Sébastien Lorion >> <[EMAIL PROTECTED]> wrote: >> > My bottom line is, if I control the data and I am 100% sure it won't >> contain >> > problematic chars, then by all means, I will use ordinal compare and get >> the >> > speed increase. Otherwise, as a non English native myself, I want to make >> my >> > apps international and so, I will use the culture aware comparison (and >> > other operations). >> >> Just be careful what you use the comparison for... Note this highly >> rated Connect "Won't Fix": >> >> >> https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=236900 >> >> I bet this'd be a bugger of a bug to locate. >> >> In this snippet you'd expect the two SortedLists to be identical, but >> they're not, and the reason's clear -- the strings are s1> >> Dim s1 As String = "-0.67:-0.33:0.33" >> Dim s2 As String = "0.67:-0.33:0.33" >> Dim s3 As String = "-0.67:0.33:-0.33" >> Console.WriteLine(s1.CompareTo(s2)) >> Console.WriteLine(s2.CompareTo(s3)) >> Console.WriteLine(s3.CompareTo(s1)) >> Dim l, m As New SortedList(Of String, Integer) >> l.Add(S1,1) >> l.Add(S2,2) >> l.Add(S3,3) >> m.Add(S3,3) >> m.Add(S2,2) >> m.Add(S1,1) >> For Each i in m >> Console.WriteLine(i) >> Next >> For Each j in l >> Console.WriteLine(j) >> Next >> >> Now I know why FXCop and the built-in VS2008 Code Analysis always says >> to specify the comparer and cultureinfo -- it's not just for performance! >> >> Regards, >> Mark Hurd, B.Sc.(Ma.) (Hons.) >> >> === >> This list is hosted by DevelopMentor(R) http://www.develop.com >> >> View archives and manage your subscription(s) at >> http://discuss.develop.com >> > > === > This list is hosted by DevelopMentor(R) http://www.develop.com > > View archives and manage your subscription(s) at http://discuss.develop.com > -- Marc C. Brooks http://musingmarc.blogspot.com
Re: [ADVANCED-DOTNET] String.Compare seems to fail when it should not
I use OrdinalIgnoreCase since we use only the ASCII characters :) At the point of the suspected code both string *should* be semantically equal. I added the comparison only for guarding reasons. Once in a while it encounters a difference ... // Ryan On Mon, Sep 8, 2008 at 7:23 PM, Sébastien Lorion <[EMAIL PROTECTED]> wrote: > What is the result if you use CurrentCultureIgnoreCase or > InvariantCultureIgnoreCase? Also, by using OrdinalIgnoreCase, you are > limiting yourself to only the first 128 chars of ASCII, which in 2008 is > kinda out of fashion... > > Sébastien > On Mon, Sep 8, 2008 at 7:25 AM, Ryan Heath <[EMAIL PROTECTED]> wrote: > >> On Mon, Sep 8, 2008 at 1:04 PM, Simon Robinson <[EMAIL PROTECTED]> >> wrote: >> >> > 1. Check the culture settings that your code is working in. I think that >> > override of String.Compare() is culture-dependant, so maybe there's some >> > unusual culture that doesn't recognize eg. 'r' and 'R' as being >> equivalent? >> >> I believe StringComparison.OrdinalIgnoreCase is culture independent >> (when it can)? >> >> > 2. Check the actual unicode values of the characters in your strings. Is >> > it possible that the 'r' (taking one random character as an example) >> > actually isn't an 'r' but is some other unusual unicode character that >> > just happens to look like an 'r' when displayed in your default font? >> >> I'll log the unicode values of the strings too, lets see what that >> will bring up. >> >> // Ryan >> >> === >> This list is hosted by DevelopMentor(R) http://www.develop.com >> >> View archives and manage your subscription(s) at >> http://discuss.develop.com >> > > === > This list is hosted by DevelopMentor(R) http://www.develop.com > > View archives and manage your subscription(s) at http://discuss.develop.com > === This list is hosted by DevelopMentor® http://www.develop.com View archives and manage your subscription(s) at http://discuss.develop.com
Re: [ADVANCED-DOTNET] String.Compare seems to fail when it should not
I especially like the comments at the bottom ... That's funny and sad at the same time :p Don't you DARE close this without fixing like you did with TypeConverter.IsValid. Posted by IDisposable1 on 11/16/2006 at 2:44 PM They dared Posted by Marc Gravell on 11/27/2006 at 10:10 PM On Mon, Sep 8, 2008 at 10:03 PM, Mark Hurd <[EMAIL PROTECTED]> wrote: > On Tue, Sep 9, 2008 at 5:57 AM, Sébastien Lorion > <[EMAIL PROTECTED]> wrote: > > My bottom line is, if I control the data and I am 100% sure it won't > contain > > problematic chars, then by all means, I will use ordinal compare and get > the > > speed increase. Otherwise, as a non English native myself, I want to make > my > > apps international and so, I will use the culture aware comparison (and > > other operations). > > Just be careful what you use the comparison for... Note this highly > rated Connect "Won't Fix": > > > https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=236900 > > I bet this'd be a bugger of a bug to locate. > > In this snippet you'd expect the two SortedLists to be identical, but > they're not, and the reason's clear -- the strings are s1 > Dim s1 As String = "-0.67:-0.33:0.33" > Dim s2 As String = "0.67:-0.33:0.33" > Dim s3 As String = "-0.67:0.33:-0.33" > Console.WriteLine(s1.CompareTo(s2)) > Console.WriteLine(s2.CompareTo(s3)) > Console.WriteLine(s3.CompareTo(s1)) > Dim l, m As New SortedList(Of String, Integer) > l.Add(S1,1) > l.Add(S2,2) > l.Add(S3,3) > m.Add(S3,3) > m.Add(S2,2) > m.Add(S1,1) > For Each i in m > Console.WriteLine(i) > Next > For Each j in l > Console.WriteLine(j) > Next > > Now I know why FXCop and the built-in VS2008 Code Analysis always says > to specify the comparer and cultureinfo -- it's not just for performance! > > Regards, > Mark Hurd, B.Sc.(Ma.) (Hons.) > > === > This list is hosted by DevelopMentor(R) http://www.develop.com > > View archives and manage your subscription(s) at > http://discuss.develop.com > === This list is hosted by DevelopMentor® http://www.develop.com View archives and manage your subscription(s) at http://discuss.develop.com
Re: [ADVANCED-DOTNET] String.Compare seems to fail when it should not
On Tue, Sep 9, 2008 at 5:57 AM, Sébastien Lorion <[EMAIL PROTECTED]> wrote: > My bottom line is, if I control the data and I am 100% sure it won't contain > problematic chars, then by all means, I will use ordinal compare and get the > speed increase. Otherwise, as a non English native myself, I want to make my > apps international and so, I will use the culture aware comparison (and > other operations). Just be careful what you use the comparison for... Note this highly rated Connect "Won't Fix": https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=236900 I bet this'd be a bugger of a bug to locate. In this snippet you'd expect the two SortedLists to be identical, but they're not, and the reason's clear -- the strings are s1http://www.develop.com View archives and manage your subscription(s) at http://discuss.develop.com
Re: [ADVANCED-DOTNET] String.Compare seems to fail when it should not
we're all being so insensitive to other cultures here! in a nutshell - if your current language settings has a character that is not the exact same value as one of the invariant characters which are all 'generic'..ex 0 1 2 3 4 5 6 7 8 9 % & * ( ) - _ = + ' " ; : . > "," < ? / a b c d e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J K L M N O P Q R S T U V W X Y Z and anything you call uses invariant - a character substitution will occur. I think this quote from ms is appropriate: "However, you should use the invariant culture only for processes that require culture-independent results, such as system services. In other cases, it produces results that might be linguistically incorrect or culturally inappropriate." lets not be culturally inappropriate either. On Mon, Sep 8, 2008 at 11:19 AM, Per Bolmstedt <[EMAIL PROTECTED] > wrote: > On Mon, 8 Sep 2008 13:23:16 -0400, =?ISO-8859-1?Q?S=E9bastien_Lorion?= > <[EMAIL PROTECTED]> wrote: > > > by using OrdinalIgnoreCase, you are limiting yourself to only the > > first 128 chars of ASCII, which in 2008 is kinda out of fashion... > > How so? > > According to "New Recommendations for Using Strings in Microsoft .NET 2.0 > "[1]; "Comparisons made using OrdinalIgnoreCase are behaviorally the > composition of two calls: calling ToUpperInvariant on both string > arguments, and doing an Ordinal comparison.". > > 1: http://msdn.microsoft.com/en-us/library/ms973919.aspx > > === > This list is hosted by DevelopMentor(R) http://www.develop.com > > View archives and manage your subscription(s) at > http://discuss.develop.com > === This list is hosted by DevelopMentor® http://www.develop.com View archives and manage your subscription(s) at http://discuss.develop.com
Re: [ADVANCED-DOTNET] String.Compare seems to fail when it should not
string s1 = "Straße"; string s2 = "STRASSE"; int ordinal = string.Compare(s1, s2, StringComparison.OrdinalIgnoreCase); // = 140 int current = string.Compare(s1, s2, StringComparison.CurrentCultureIgnoreCase); // = 0 OrdinalIgnoreCase does not use Unicode conversion tables for making letters uppercase. It makes it faster, but also may produce unexpected results. The end result for non-english users is annoying. Sébastien On Mon, Sep 8, 2008 at 3:02 PM, Per Bolmstedt <[EMAIL PROTECTED]>wrote: > Define "won't work". Also, can you shed some more light on why it "won't > work"? > > On Mon, 8 Sep 2008 14:32:02 -0400, =?ISO-8859-1?Q?S=E9bastien_Lorion?= > <[EMAIL PROTECTED]> wrote: > > >It won't work with international characters. > > > >Sébastien > >On Mon, Sep 8, 2008 at 2:19 PM, Per Bolmstedt > ><[EMAIL PROTECTED]>wrote: > > > >> On Mon, 8 Sep 2008 13:23:16 -0400, =?ISO-8859-1?Q?S=E9bastien_Lorion?= > >> <[EMAIL PROTECTED]> wrote: > >> > >>> by using OrdinalIgnoreCase, you are limiting yourself to only the > >>> first 128 chars of ASCII, which in 2008 is kinda out of fashion... > >> > >> How so? > >> > >> According to "New Recommendations for Using Strings in Microsoft .NET > >> "[1]; "Comparisons made using OrdinalIgnoreCase are behaviorally the > >> composition of two calls: calling ToUpperInvariant on both string > >> arguments, and doing an Ordinal comparison.". > >> > >> 1: http://msdn.microsoft.com/en-us/library/ms973919.aspx > > === > This list is hosted by DevelopMentor(R) http://www.develop.com > > View archives and manage your subscription(s) at > http://discuss.develop.com > === This list is hosted by DevelopMentor® http://www.develop.com View archives and manage your subscription(s) at http://discuss.develop.com
Re: [ADVANCED-DOTNET] String.Compare seems to fail when it should not
My bottom line is, if I control the data and I am 100% sure it won't contain problematic chars, then by all means, I will use ordinal compare and get the speed increase. Otherwise, as a non English native myself, I want to make my apps international and so, I will use the culture aware comparison (and other operations). Sébastien On Mon, Sep 8, 2008 at 3:21 PM, Sébastien Lorion <[EMAIL PROTECTED] > wrote: > string s1 = "Straße"; > string s2 = "STRASSE"; > > int ordinal = string.Compare(s1, s2, StringComparison.OrdinalIgnoreCase); > // = 140 > int current = string.Compare(s1, s2, > StringComparison.CurrentCultureIgnoreCase); // = 0 > > OrdinalIgnoreCase does not use Unicode conversion tables for making letters > uppercase. It makes it faster, but also may produce unexpected results. The > end result for non-english users is annoying. > > Sébastien > On Mon, Sep 8, 2008 at 3:02 PM, Per Bolmstedt ul7.info> wrote: > >> Define "won't work". Also, can you shed some more light on why it "won't >> work"? >> >> On Mon, 8 Sep 2008 14:32:02 -0400, =?ISO-8859-1?Q?S=E9bastien_Lorion?= >> <[EMAIL PROTECTED]> wrote: >> >> >It won't work with international characters. >> > >> >Sébastien >> >On Mon, Sep 8, 2008 at 2:19 PM, Per Bolmstedt >> ><[EMAIL PROTECTED]>wrote: >> > >> >> On Mon, 8 Sep 2008 13:23:16 -0400, =?ISO-8859-1?Q?S=E9bastien_Lorion?= >> >> <[EMAIL PROTECTED]> wrote: >> >> >> >>> by using OrdinalIgnoreCase, you are limiting yourself to only the >> >>> first 128 chars of ASCII, which in 2008 is kinda out of fashion... >> >> >> >> How so? >> >> >> >> According to "New Recommendations for Using Strings in Microsoft .NET >> >> "[1]; "Comparisons made using OrdinalIgnoreCase are behaviorally the >> >> composition of two calls: calling ToUpperInvariant on both string >> >> arguments, and doing an Ordinal comparison.". >> >> >> >> 1: http://msdn.microsoft.com/en-us/library/ms973919.aspx >> >> === >> This list is hosted by DevelopMentor(R) http://www.develop.com >> >> View archives and manage your subscription(s) at >> http://discuss.develop.com >> > > === This list is hosted by DevelopMentor® http://www.develop.com View archives and manage your subscription(s) at http://discuss.develop.com
Re: [ADVANCED-DOTNET] String.Compare seems to fail when it should not
Define "won't work". Also, can you shed some more light on why it "won't work"? On Mon, 8 Sep 2008 14:32:02 -0400, =?ISO-8859-1?Q?S=E9bastien_Lorion?= <[EMAIL PROTECTED]> wrote: >It won't work with international characters. > >Sébastien >On Mon, Sep 8, 2008 at 2:19 PM, Per Bolmstedt ><[EMAIL PROTECTED]>wrote: > >> On Mon, 8 Sep 2008 13:23:16 -0400, =?ISO-8859-1?Q?S=E9bastien_Lorion?= >> <[EMAIL PROTECTED]> wrote: >> >>> by using OrdinalIgnoreCase, you are limiting yourself to only the >>> first 128 chars of ASCII, which in 2008 is kinda out of fashion... >> >> How so? >> >> According to "New Recommendations for Using Strings in Microsoft .NET >> "[1]; "Comparisons made using OrdinalIgnoreCase are behaviorally the >> composition of two calls: calling ToUpperInvariant on both string >> arguments, and doing an Ordinal comparison.". >> >> 1: http://msdn.microsoft.com/en-us/library/ms973919.aspx === This list is hosted by DevelopMentor® http://www.develop.com View archives and manage your subscription(s) at http://discuss.develop.com
Re: [ADVANCED-DOTNET] String.Compare seems to fail when it should not
It won't work with international characters. Sébastien On Mon, Sep 8, 2008 at 2:19 PM, Per Bolmstedt <[EMAIL PROTECTED]>wrote: > On Mon, 8 Sep 2008 13:23:16 -0400, =?ISO-8859-1?Q?S=E9bastien_Lorion?= > <[EMAIL PROTECTED]> wrote: > > > by using OrdinalIgnoreCase, you are limiting yourself to only the > > first 128 chars of ASCII, which in 2008 is kinda out of fashion... > > How so? > > According to "New Recommendations for Using Strings in Microsoft .NET 2.0 > "[1]; "Comparisons made using OrdinalIgnoreCase are behaviorally the > composition of two calls: calling ToUpperInvariant on both string > arguments, and doing an Ordinal comparison.". > > 1: http://msdn.microsoft.com/en-us/library/ms973919.aspx > > === > This list is hosted by DevelopMentor(R) http://www.develop.com > > View archives and manage your subscription(s) at > http://discuss.develop.com > === This list is hosted by DevelopMentor® http://www.develop.com View archives and manage your subscription(s) at http://discuss.develop.com
Re: [ADVANCED-DOTNET] String.Compare seems to fail when it should not
On Mon, 8 Sep 2008 13:23:16 -0400, =?ISO-8859-1?Q?S=E9bastien_Lorion?= <[EMAIL PROTECTED]> wrote: > by using OrdinalIgnoreCase, you are limiting yourself to only the > first 128 chars of ASCII, which in 2008 is kinda out of fashion... How so? According to "New Recommendations for Using Strings in Microsoft .NET 2.0 "[1]; "Comparisons made using OrdinalIgnoreCase are behaviorally the composition of two calls: calling ToUpperInvariant on both string arguments, and doing an Ordinal comparison.". 1: http://msdn.microsoft.com/en-us/library/ms973919.aspx === This list is hosted by DevelopMentor® http://www.develop.com View archives and manage your subscription(s) at http://discuss.develop.com
Re: [ADVANCED-DOTNET] String.Compare seems to fail when it should not
What is the result if you use CurrentCultureIgnoreCase or InvariantCultureIgnoreCase? Also, by using OrdinalIgnoreCase, you are limiting yourself to only the first 128 chars of ASCII, which in 2008 is kinda out of fashion... Sébastien On Mon, Sep 8, 2008 at 7:25 AM, Ryan Heath <[EMAIL PROTECTED]> wrote: > On Mon, Sep 8, 2008 at 1:04 PM, Simon Robinson <[EMAIL PROTECTED]> > wrote: > > > 1. Check the culture settings that your code is working in. I think that > > override of String.Compare() is culture-dependant, so maybe there's some > > unusual culture that doesn't recognize eg. 'r' and 'R' as being > equivalent? > > I believe StringComparison.OrdinalIgnoreCase is culture independent > (when it can)? > > > 2. Check the actual unicode values of the characters in your strings. Is > > it possible that the 'r' (taking one random character as an example) > > actually isn't an 'r' but is some other unusual unicode character that > > just happens to look like an 'r' when displayed in your default font? > > I'll log the unicode values of the strings too, lets see what that > will bring up. > > // Ryan > > === > This list is hosted by DevelopMentor(R) http://www.develop.com > > View archives and manage your subscription(s) at > http://discuss.develop.com > === This list is hosted by DevelopMentor® http://www.develop.com View archives and manage your subscription(s) at http://discuss.develop.com
Re: [ADVANCED-DOTNET] String.Compare seems to fail when it should not
On Mon, Sep 8, 2008 at 1:04 PM, Simon Robinson <[EMAIL PROTECTED]> wrote: > 1. Check the culture settings that your code is working in. I think that > override of String.Compare() is culture-dependant, so maybe there's some > unusual culture that doesn't recognize eg. 'r' and 'R' as being equivalent? I believe StringComparison.OrdinalIgnoreCase is culture independent (when it can)? > 2. Check the actual unicode values of the characters in your strings. Is > it possible that the 'r' (taking one random character as an example) > actually isn't an 'r' but is some other unusual unicode character that > just happens to look like an 'r' when displayed in your default font? I'll log the unicode values of the strings too, lets see what that will bring up. // Ryan === This list is hosted by DevelopMentor® http://www.develop.com View archives and manage your subscription(s) at http://discuss.develop.com
Re: [ADVANCED-DOTNET] String.Compare seems to fail when it should not
On Mon, Sep 8, 2008 at 12:58 PM, Per Bolmstedt
<[EMAIL PROTECTED]> wrote:
> This sounds like a non-sequitur to me, since I would have expected the
> lengths to be the same, even if the contents differ.
I was thinking more of "roma{null}garbage", when printed it would
print "roma" but its length would be 12 instead of 4.
> Are you sure "romA" and "ROMA" in your example really contain the same
> characters? There are
> several Unicode code points to achieve the same result for at least one of
> those letters. Can you find two strings that don't Compare() to 0 and print
> the Unicode code point for each letter?
Thanks I'll try that.
// Ryan
===
This list is hosted by DevelopMentor® http://www.develop.com
View archives and manage your subscription(s) at http://discuss.develop.com
Re: [ADVANCED-DOTNET] String.Compare seems to fail when it should not
Well if you're certain you've checked the code for typos etc. and it's
failing as you claim, that does seem odd :) The only possibilities I can
think of (and both of these seem like very long shots) are...
1. Check the culture settings that your code is working in. I think that
override of String.Compare() is culture-dependant, so maybe there's some
unusual culture that doesn't recognize eg. 'r' and 'R' as being equivalent?
2. Check the actual unicode values of the characters in your strings. Is
it possible that the 'r' (taking one random character as an example)
actually isn't an 'r' but is some other unusual unicode character that
just happens to look like an 'r' when displayed in your default font?
Simon
>Yeah, that will teach me to post some contrived code to the list :)
>But the production code is really working that way, String.Compare doesn't
>return zero for
>strings that seem to be equally to me.
>
>At first I thought the internal representation of the strings where not
>equal, so I logged the length of the strings.
>But even their lengths are the same! :S
>
>Anyone another idea how to tackle this?
>
>A lot of entries that fail contain an i, so I immediately thought about
the
>turkish-i problem,
>but I have found another entry that failed to without an i in it:
>romA vs ROMA
>
>// Ryan
>
>On Fri, Sep 5, 2008 at 4:54 PM, Simon Robinson
<[EMAIL PROTECTED]>wrote:
>
>> It works fine for me (other than that your code doesn't compile because
>> you need String.Format() inside the exception constructor).
>>
>> Are you sure there isn't at typo or something in your code?
>>
>> My test code is:
>>
>> // code works fine - string.Compare() returns 0 so no exception
>> string str1 = "-mylife";
>> string str2 = "-MYLIFE";
>> if (0 != string.Compare(str1, str2, StringComparison.OrdinalIgnoreCase))
>> {
>> string msg = string.Format("Compare failed str1:({0}) length:{1}
>> str2:({2}) length:{3}", str1, str1.Length, str2, str2.Length);
>> throw new Exception(msg);
>> }
>>
>> ===
>> This list is hosted by DevelopMentor(R) http://www.develop.com
>>
>> View archives and manage your subscription(s) at
>> http://discuss.develop.com
>>
>
>===
>This list is hosted by DevelopMentor® http://www.develop.com
>
>View archives and manage your subscription(s) at
http://discuss.develop.com
===
This list is hosted by DevelopMentor® http://www.develop.com
View archives and manage your subscription(s) at http://discuss.develop.com
Re: [ADVANCED-DOTNET] String.Compare seems to fail when it should not
On Mon, 8 Sep 2008 12:38:32 +0200, Ryan Heath <[EMAIL PROTECTED]> wrote: > At first I thought the internal representation of the strings where > not equal, so I logged the length of the strings. But even their > lengths are the same! :S This sounds like a non-sequitur to me, since I would have expected the lengths to be the same, even if the contents differ. Are you sure "romA" and "ROMA" in your example really contain the same characters? There are several Unicode code points to achieve the same result for at least one of those letters. Can you find two strings that don't Compare() to 0 and print the Unicode code point for each letter? === This list is hosted by DevelopMentor® http://www.develop.com View archives and manage your subscription(s) at http://discuss.develop.com
Re: [ADVANCED-DOTNET] String.Compare seems to fail when it should not
Yeah, that will teach me to post some contrived code to the list :)
But the production code is really working that way, String.Compare doesn't
return zero for
strings that seem to be equally to me.
At first I thought the internal representation of the strings where not
equal, so I logged the length of the strings.
But even their lengths are the same! :S
Anyone another idea how to tackle this?
A lot of entries that fail contain an i, so I immediately thought about the
turkish-i problem,
but I have found another entry that failed to without an i in it:
romA vs ROMA
// Ryan
On Fri, Sep 5, 2008 at 4:54 PM, Simon Robinson <[EMAIL PROTECTED]>wrote:
> It works fine for me (other than that your code doesn't compile because
> you need String.Format() inside the exception constructor).
>
> Are you sure there isn't at typo or something in your code?
>
> My test code is:
>
> // code works fine - string.Compare() returns 0 so no exception
> string str1 = "-mylife";
> string str2 = "-MYLIFE";
> if (0 != string.Compare(str1, str2, StringComparison.OrdinalIgnoreCase))
> {
> string msg = string.Format("Compare failed str1:({0}) length:{1}
> str2:({2}) length:{3}", str1, str1.Length, str2, str2.Length);
> throw new Exception(msg);
> }
>
> ===
> This list is hosted by DevelopMentor(R) http://www.develop.com
>
> View archives and manage your subscription(s) at
> http://discuss.develop.com
>
===
This list is hosted by DevelopMentor® http://www.develop.com
View archives and manage your subscription(s) at http://discuss.develop.com
Re: [ADVANCED-DOTNET] String.Compare seems to fail when it should not
> Correct me as I may be all wrong but I thought dotnet recommends that > you > not count on -1,0,1 being indicators for true and false? Have I got > this > all wrong (extremely likely)? > > John Warner They do, but they use them here to indicate not just true or false, but also whether one string is 'higher' or lower than the other one. String.Equals() returns a bool. === This list is hosted by DevelopMentor® http://www.develop.com View archives and manage your subscription(s) at http://discuss.develop.com
Re: [ADVANCED-DOTNET] String.Compare seems to fail when it should not
It works fine for me (other than that your code doesn't compile because
you need String.Format() inside the exception constructor).
Are you sure there isn't at typo or something in your code?
My test code is:
// code works fine - string.Compare() returns 0 so no exception
string str1 = "-mylife";
string str2 = "-MYLIFE";
if (0 != string.Compare(str1, str2, StringComparison.OrdinalIgnoreCase))
{
string msg = string.Format("Compare failed str1:({0}) length:{1}
str2:({2}) length:{3}", str1, str1.Length, str2, str2.Length);
throw new Exception(msg);
}
===
This list is hosted by DevelopMentor® http://www.develop.com
View archives and manage your subscription(s) at http://discuss.develop.com
Re: [ADVANCED-DOTNET] String.Compare seems to fail when it should not
Correct me as I may be all wrong but I thought dotnet recommends that you not count on -1,0,1 being indicators for true and false? Have I got this all wrong (extremely likely)? John Warner > -Original Message- > From: Discussion of advanced .NET topics. > [mailto:[EMAIL PROTECTED] On Behalf Of Ryan Heath > Sent: Friday, September 05, 2008 10:18 AM > To: [email protected] > Subject: [ADVANCED-DOTNET] String.Compare seems to fail when > it should not > > > Hi, > > See following snippet, > > if (0 != string.Compare(str1, str2, > StringComparison.OrdinalIgnoreCase)) > { > throw new Exception("Compare failed str1:({0}) length:{1} > str2:({2}) length:{3}", str1, str1.Length, str2, str2.Length); } > > it throws, for instance, with "Compare failed str1:(-mylife) length:7 > str2:(-MYLIFE) length:7" > > Obviously something is really wrong here... > Has anyone seen this behavior before, or has anyone some tips > how to investigate this problem? > > // Ryan > > === > This list is hosted by DevelopMentorR http://www.develop.com > > View archives and manage your subscription(s) at > http://discuss.develop.com > === This list is hosted by DevelopMentor® http://www.develop.com View archives and manage your subscription(s) at http://discuss.develop.com
