Comparing Strings

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jonathan Wood

    Comparing Strings

    Can someone tell me how to compare two strings using a case-insensitive
    comparison.

    Ideally, I would prefer not to have to change a global setting or override
    any other interfaces.

    BTW, does anyone know why documentation for .NET library methods don't have
    a Return Value section where the return value is described? Seems really
    strange to me that Microsoft didn't feel return values needed to be
    documented.

    --
    Jonathan Wood
    SoftCircuits



  • Naveen

    #2
    Re: Comparing Strings

    Here is a official guide to string comparison .NET 2.0

    .NET is a developer platform with tools and libraries for building any type of app, including web, mobile, desktop, games, IoT, cloud, and microservices.


    Comment

    • Ignacio Machin \( .NET/ C# MVP \)

      #3
      Re: Comparing Strings

      Hi,


      "Jonathan Wood" <jwood@softcirc uits.com> wrote in message
      news:u5553TrIGH A.3700@TK2MSFTN GP15.phx.gbl...[color=blue]
      > Can someone tell me how to compare two strings using a case-insensitive
      > comparison.
      >
      > Ideally, I would prefer not to have to change a global setting or override
      > any other interfaces.
      >[/color]
      Hi, IIRC in v 2.0 there is a new recommended way to handle string
      comparisions
      Ok, I found it in google :



      [color=blue]
      > BTW, does anyone know why documentation for .NET library methods don't
      > have a Return Value section where the return value is described? Seems
      > really strange to me that Microsoft didn't feel return values needed to be
      > documented.[/color]

      Hi,

      In general you do have it, only in some cases you do not have that section,
      note that this is only for methods (not properties) that return something


      --
      Ignacio Machin,
      ignacio.machin AT dot.state.fl.us
      Florida Department Of Transportation


      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: Comparing Strings

        Jonathan Wood <jwood@softcirc uits.com> wrote:[color=blue]
        > Can someone tell me how to compare two strings using a case-insensitive
        > comparison.[/color]

        In 2.0, use String.Compare (first, second,
        StringCompariso n.OrdinalIgnore Case)

        (Or CurrentCultureI gnoreCase, or InvariantCultur eIgnoreCase)

        In 1.1, use String.Compare (first, second, true) if you're happy using
        the current culture.
        [color=blue]
        > Ideally, I would prefer not to have to change a global setting or override
        > any other interfaces.
        >
        > BTW, does anyone know why documentation for .NET library methods don't have
        > a Return Value section where the return value is described? Seems really
        > strange to me that Microsoft didn't feel return values needed to be
        > documented.[/color]

        It does. In the case of String.Compare( string,string,S tringComparison )
        it's:

        <quote>
        Return Value
        A 32-bit signed integer indicating the lexical relationship between the
        two comparands.
        Value
        Condition

        Less than zero
        strA is less than strB.

        Zero
        strA equals strB.

        Greater than zero
        strA is greater than strB.
        </quote>

        --
        Jon Skeet - <skeet@pobox.co m>
        http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
        If replying to the group, please do not mail me too

        Comment

        • Jonathan Wood

          #5
          Re: Comparing Strings

          Hi Jon,
          [color=blue]
          > In 1.1, use String.Compare (first, second, true) if you're happy using
          > the current culture.[/color]

          Ack! Okay, I see that now.

          Can someone explain to me why we're using this syntax? I guess Compare is a
          static function. But why is it not part of the string class? (i.e., string
          s; s.Compare(first , second, true) or s.Compare(secon d, true)?

          Thanks.
          [color=blue]
          > It does. In the case of String.Compare( string,string,S tringComparison )
          > it's:[/color]

          A few do. Many do not. For example:


          --
          Jonathan Wood
          SoftCircuits

          Available for consulting: http://www.softcircuits.com/jwood/resume.htm


          Comment

          • Jon Skeet [C# MVP]

            #6
            Re: Comparing Strings

            Jonathan Wood <jwood@softcirc uits.com> wrote:[color=blue][color=green]
            > > In 1.1, use String.Compare (first, second, true) if you're happy using
            > > the current culture.[/color]
            >
            > Ack! Okay, I see that now.
            >
            > Can someone explain to me why we're using this syntax? I guess Compare is a
            > static function. But why is it not part of the string class? (i.e., string
            > s; s.Compare(first , second, true) or s.Compare(secon d, true)?[/color]

            Well, having it as a static method means it's available without you
            having to worry about whether or not either side is null. Yes, each of
            the overloads *could* be available as an instance method too, but I'm
            not sure it would give very much benefit.
            [color=blue][color=green]
            > > It does. In the case of String.Compare( string,string,S tringComparison )
            > > it's:[/color]
            >
            > A few do. Many do not. For example:
            > http://msdn.microsoft.com/library/de...ary/en-us/cpre
            > f/html/frlrfSystemColl ectionsArrayLis tClassBinarySea rchTopic.asp[/color]

            Every one of those overloads has a "Return value" section. It's only
            the page listing the overloads which doesn't.

            --
            Jon Skeet - <skeet@pobox.co m>
            http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
            If replying to the group, please do not mail me too

            Comment

            • Jonathan Wood

              #7
              Re: Comparing Strings

              Jon,
              [color=blue][color=green]
              >> Can someone explain to me why we're using this syntax? I guess Compare is
              >> a
              >> static function. But why is it not part of the string class? (i.e.,
              >> string
              >> s; s.Compare(first , second, true) or s.Compare(secon d, true)?[/color]
              >
              > Well, having it as a static method means it's available without you
              > having to worry about whether or not either side is null. Yes, each of
              > the overloads *could* be available as an instance method too, but I'm
              > not sure it would give very much benefit.[/color]

              I guess. It's just seems less OOP to me this way.

              Thanks.

              --
              Jonathan Wood
              SoftCircuits



              Comment

              • Jeff Louie

                #8
                Re: Comparing Strings

                It is a less "object oriented style of computation", but x.is_equal(y)
                failed in the
                real world spawning the equal(x,y) convention. pp275
                OOSoftwareConst ruction
                ed.2 Meyer.

                Regards,
                Jeff

                *** Sent via Developersdex http://www.developersdex.com ***

                Comment

                Working...