Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. System.String quiz

System.String quiz

Scheduled Pinned Locked Moved The Lounge
10 Posts 6 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Daniel Grunwald
    wrote on last edited by
    #1

    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));
    }

    J R P 3 Replies Last reply
    0
    • D Daniel Grunwald

      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));
      }

      J Offline
      J Offline
      Jason Lepack LeppyR64
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      • D Daniel Grunwald

        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));
        }

        R Offline
        R Offline
        Rama Krishna Vavilala
        wrote on last edited by
        #3

        My guess is it may have something to do with the Culture.

        S 1 Reply Last reply
        0
        • D Daniel Grunwald

          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));
          }

          P Offline
          P Offline
          Phil Martin
          wrote on last edited by
          #4

          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.

          L D 2 Replies Last reply
          0
          • P Phil Martin

            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.

            L Offline
            L Offline
            leppie
            wrote on last edited by
            #5

            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))

            P 1 Reply Last reply
            0
            • L leppie

              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))

              P Offline
              P Offline
              Phil Martin
              wrote on last edited by
              #6

              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.

              L 1 Reply Last reply
              0
              • P Phil Martin

                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.

                L Offline
                L Offline
                leppie
                wrote on last edited by
                #7

                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))

                1 Reply Last reply
                0
                • R Rama Krishna Vavilala

                  My guess is it may have something to do with the Culture.

                  S Offline
                  S Offline
                  Stuart Dootson
                  wrote on last edited by
                  #8

                  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

                  1 Reply Last reply
                  0
                  • P Phil Martin

                    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.

                    D Offline
                    D Offline
                    Daniel Grunwald
                    wrote on last edited by
                    #9

                    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!)

                    P 1 Reply Last reply
                    0
                    • D Daniel Grunwald

                      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!)

                      P Offline
                      P Offline
                      Phil Martin
                      wrote on last edited by
                      #10

                      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

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups