System.String quiz
-
Find inputs (a,b) so that that following function returns true:
static bool IsSystemStringCrazy(string a, string b)
{
return a.StartsWith(b) && (!a.Contains(b)) && b.StartsWith(a) && (!a.Equals(b));
} -
Find inputs (a,b) so that that following function returns true:
static bool IsSystemStringCrazy(string a, string b)
{
return a.StartsWith(b) && (!a.Contains(b)) && b.StartsWith(a) && (!a.Equals(b));
}This is a joke right? a.StartsWith(b) returns true if a = bx b.StartsWith(a) returns true if b = ay a != ayx b != bxy unless x = "" and y = "" So therefore the only way this could return true is if a == b, but you've removed that possibility, so this can't possibly return true. However, since you're posting this, there's either something stupid up, or you're jsut posting it because you feel like an idiot? No offense intended :D
-
Find inputs (a,b) so that that following function returns true:
static bool IsSystemStringCrazy(string a, string b)
{
return a.StartsWith(b) && (!a.Contains(b)) && b.StartsWith(a) && (!a.Equals(b));
}My guess is it may have something to do with the Culture.
-
Find inputs (a,b) so that that following function returns true:
static bool IsSystemStringCrazy(string a, string b)
{
return a.StartsWith(b) && (!a.Contains(b)) && b.StartsWith(a) && (!a.Equals(b));
}I would have to go with: IsSystemStringCrazy("Æ", "AE") running in the en-US (or probably any english) culture. And the even more interesting thing is I think in .Net 4.0 this will return false.
-
I would have to go with: IsSystemStringCrazy("Æ", "AE") running in the en-US (or probably any english) culture. And the even more interesting thing is I think in .Net 4.0 this will return false.
Phil Martin... wrote:
And the even more interesting thing is I think in .Net 4.0 this will return false.
They getting rid of Unicode? ;P
xacc.ide - now with TabsToSpaces support
IronScheme - 1.0 beta 2 - out now!
((lambda (x) `((lambda (x) ,x) ',x)) '`((lambda (x) ,x) ',x)) -
Phil Martin... wrote:
And the even more interesting thing is I think in .Net 4.0 this will return false.
They getting rid of Unicode? ;P
xacc.ide - now with TabsToSpaces support
IronScheme - 1.0 beta 2 - out now!
((lambda (x) `((lambda (x) ,x) ',x)) '`((lambda (x) ,x) ',x))Yeah, I think they are replacing it with UltraCode. Each character is stored as a 128 bits, with the first 16 bits used to define the size of the font to render it with. Because we all know that saying "Danger" in tiny letters is totally different to when it's written in huge letters, so .Equals should return false. But what I was really talking about was this/a>[^] They are changing the default comparison behavior for StartsWith, EndsWith, IndexOf, and LastIndexOf, so they are using ordinals instead of culture sensitive.
-
Yeah, I think they are replacing it with UltraCode. Each character is stored as a 128 bits, with the first 16 bits used to define the size of the font to render it with. Because we all know that saying "Danger" in tiny letters is totally different to when it's written in huge letters, so .Equals should return false. But what I was really talking about was this/a>[^] They are changing the default comparison behavior for StartsWith, EndsWith, IndexOf, and LastIndexOf, so they are using ordinals instead of culture sensitive.
Phil Martin... wrote:
They are changing the default comparison behavior for StartsWith, EndsWith, IndexOf, and LastIndexOf, so they are using ordinals instead of culture sensitive. Quote Selected Text
Ahh that makes sense :) Thanks.
xacc.ide - now with TabsToSpaces support
IronScheme - 1.0 beta 2 - out now!
((lambda (x) `((lambda (x) ,x) ',x)) '`((lambda (x) ,x) ',x)) -
My guess is it may have something to do with the Culture.
Rama Krishna Vavilala wrote:
the Culture
I thought they were merely a literary device[^]? They're real and affecting .NET? Damn, Iain Banks deserves credit for his vision.
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
-
I would have to go with: IsSystemStringCrazy("Æ", "AE") running in the en-US (or probably any english) culture. And the even more interesting thing is I think in .Net 4.0 this will return false.
Correct! I was thinking about another ligature ("ß"[^], "ss"), which also returns true in en-US (even though ß is not an English character). But, you don't even need ligatures. You can also use any compound character in different normalization forms: IsSystemStringCrazy("é".Normalize(NormalizationForm.FormC), "é".Normalize(NormalizationForm.FormD)) The problem here is that the default StringComparison is more or less random: some methods (Equals, Contains) or Ordinal by default, others use CurrentCulture. But this is unrelated to any specific culture, it's just the difference between ordinal and culture-aware comparisons. If you're using using culture-based comparsions for file names (including InvariantCulture), there are some surprises waiting for you: - You'll fail when there a two files with (culture-based) equals names: "Grüße.txt" vs "Grüsse.txt" - Oh, and if you ever run across strings in normalization form D (e.g. files from a Mac), convert them to normalization form C before using them as Windows file names. Oh, and now the bonus question: Is there string a so that "string.Empty.StartsWith(a) && a.Length > 0" ? (yes, there is!)
-
Correct! I was thinking about another ligature ("ß"[^], "ss"), which also returns true in en-US (even though ß is not an English character). But, you don't even need ligatures. You can also use any compound character in different normalization forms: IsSystemStringCrazy("é".Normalize(NormalizationForm.FormC), "é".Normalize(NormalizationForm.FormD)) The problem here is that the default StringComparison is more or less random: some methods (Equals, Contains) or Ordinal by default, others use CurrentCulture. But this is unrelated to any specific culture, it's just the difference between ordinal and culture-aware comparisons. If you're using using culture-based comparsions for file names (including InvariantCulture), there are some surprises waiting for you: - You'll fail when there a two files with (culture-based) equals names: "Grüße.txt" vs "Grüsse.txt" - Oh, and if you ever run across strings in normalization form D (e.g. files from a Mac), convert them to normalization form C before using them as Windows file names. Oh, and now the bonus question: Is there string a so that "string.Empty.StartsWith(a) && a.Length > 0" ? (yes, there is!)
I'm sure there are more than a few, but here's the lowest unicode character that does it: string.Empty.StartsWith("\x01f6") is true