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. Other Discussions
  3. The Weird and The Wonderful
  4. More on string comparison

More on string comparison

Scheduled Pinned Locked Moved The Weird and The Wonderful
csharp
12 Posts 5 Posters 1 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.
  • O oggenok64

    Straight out of a C# Unified Communications code sample from MS:

    if (string.IsNullOrEmpty(customMessage.Trim()))
    ...

    B Offline
    B Offline
    Bernhard Hiller
    wrote on last edited by
    #2

    Looks like somewhen Trim() returned null when a string contained spaces only. Or was it meant for Oracle compatibility (with Oracle, an empty string means null)?

    1 Reply Last reply
    0
    • O oggenok64

      Straight out of a C# Unified Communications code sample from MS:

      if (string.IsNullOrEmpty(customMessage.Trim()))
      ...

      _ Offline
      _ Offline
      _Erik_
      wrote on last edited by
      #3

      Really funny. The check for a null string will throw a null reference exception is customMessage is null.

      1 Reply Last reply
      0
      • O oggenok64

        Straight out of a C# Unified Communications code sample from MS:

        if (string.IsNullOrEmpty(customMessage.Trim()))
        ...

        M Offline
        M Offline
        musefan
        wrote on last edited by
        #4

        What is wrong with that? I imagine this was before .Net 4.0 in which you can now use string.IsNullOrWhitespace()...

        if(string.IsNullOrWhitespace(customMessage))
        ...

        Illogical thoughts make me ill

        S 1 Reply Last reply
        0
        • M musefan

          What is wrong with that? I imagine this was before .Net 4.0 in which you can now use string.IsNullOrWhitespace()...

          if(string.IsNullOrWhitespace(customMessage))
          ...

          Illogical thoughts make me ill

          S Offline
          S Offline
          Samuel Cragg
          wrote on last edited by
          #5

          musefan wrote:

          What is wrong with that?

          As _Erik_ said, what happens with this code?

          string customMessage = null;
          if (string.IsNullOrEmpty(customMessage.Trim()))
          ...

          M 1 Reply Last reply
          0
          • S Samuel Cragg

            musefan wrote:

            What is wrong with that?

            As _Erik_ said, what happens with this code?

            string customMessage = null;
            if (string.IsNullOrEmpty(customMessage.Trim()))
            ...

            M Offline
            M Offline
            musefan
            wrote on last edited by
            #6

            now that's shameful coding, but the OP never put that, there is no evidence that it is unsafe to assume that customMessage will never be null. What if the following was the case...

            string customMessage = GetCustomMessage();
            if (string.IsNullOrEmpty(customMessage.Trim()))
            ...

            string GetCustomMessage(){
            string result = GetSomeDataFromSomewhere();
            return result ?? "";//or in times of old... return result == null ? "" : result;
            }

            Illogical thoughts make me ill

            S 1 Reply Last reply
            0
            • M musefan

              now that's shameful coding, but the OP never put that, there is no evidence that it is unsafe to assume that customMessage will never be null. What if the following was the case...

              string customMessage = GetCustomMessage();
              if (string.IsNullOrEmpty(customMessage.Trim()))
              ...

              string GetCustomMessage(){
              string result = GetSomeDataFromSomewhere();
              return result ?? "";//or in times of old... return result == null ? "" : result;
              }

              Illogical thoughts make me ill

              S Offline
              S Offline
              Samuel Cragg
              wrote on last edited by
              #7

              musefan wrote:

              there is no evidence that it is unsafe to assume that customMessage will never be null

              If that was the case then the call to string.IsNullOrEmpty would not be required, as you could safely access the Length property and test for zero. Eitherway you look at it, the OP code is unnecessary ;)

              M 1 Reply Last reply
              0
              • S Samuel Cragg

                musefan wrote:

                there is no evidence that it is unsafe to assume that customMessage will never be null

                If that was the case then the call to string.IsNullOrEmpty would not be required, as you could safely access the Length property and test for zero. Eitherway you look at it, the OP code is unnecessary ;)

                M Offline
                M Offline
                musefan
                wrote on last edited by
                #8

                But the whole point in the trim (IMO) is to test that a complete whitespace string is not confused with meaningful data. Imagine if the string was fetched using my sample method, and the call to get the data in there returns a tab separated list - this function may always end in a tab even when no data has been added. so the resulting value could be "\t" in which case it should be ignored just like "" would be. Your length test doesn't work in this case because a string of data and whitespace would equal the same. Consider the following possible string values and what you want to test for invalid...

                string s = "";//empty and want to handle (length is 0 so fine)
                string s = "\t";//as good as empty and want to handle (length is 1 so problem here)
                string s = @"data 1\tdata 2\t";//some useful data and this will not be 'caught' by the trim test

                Illogical thoughts make me ill

                S 1 Reply Last reply
                0
                • M musefan

                  But the whole point in the trim (IMO) is to test that a complete whitespace string is not confused with meaningful data. Imagine if the string was fetched using my sample method, and the call to get the data in there returns a tab separated list - this function may always end in a tab even when no data has been added. so the resulting value could be "\t" in which case it should be ignored just like "" would be. Your length test doesn't work in this case because a string of data and whitespace would equal the same. Consider the following possible string values and what you want to test for invalid...

                  string s = "";//empty and want to handle (length is 0 so fine)
                  string s = "\t";//as good as empty and want to handle (length is 1 so problem here)
                  string s = @"data 1\tdata 2\t";//some useful data and this will not be 'caught' by the trim test

                  Illogical thoughts make me ill

                  S Offline
                  S Offline
                  Samuel Cragg
                  wrote on last edited by
                  #9

                  Sorry, I don't think I phrased it well. The length test was a replacement for the string.IsNullOrEmpty call - you can still call Trim though. Back to the original code:

                  // This is the same as the original code - if customMessage
                  // is null then they'll be an exception
                  if (customMessage.Trim().Length == 0)
                  ...

                  M 1 Reply Last reply
                  0
                  • S Samuel Cragg

                    Sorry, I don't think I phrased it well. The length test was a replacement for the string.IsNullOrEmpty call - you can still call Trim though. Back to the original code:

                    // This is the same as the original code - if customMessage
                    // is null then they'll be an exception
                    if (customMessage.Trim().Length == 0)
                    ...

                    M Offline
                    M Offline
                    musefan
                    wrote on last edited by
                    #10

                    I see, yep that's OK with me :) My initial post was really to find out why it was such a bad line of code when we cannot see any other code to judge. People seemed to be going for the null exception but there was nothing to indicate that wouldcould happen

                    Illogical thoughts make me ill

                    O 1 Reply Last reply
                    0
                    • M musefan

                      I see, yep that's OK with me :) My initial post was really to find out why it was such a bad line of code when we cannot see any other code to judge. People seemed to be going for the null exception but there was nothing to indicate that wouldcould happen

                      Illogical thoughts make me ill

                      O Offline
                      O Offline
                      oggenok64
                      wrote on last edited by
                      #11

                      However unlikely, things that CAN happen eventually WILL happen. Illogical or not.

                      M 1 Reply Last reply
                      0
                      • O oggenok64

                        However unlikely, things that CAN happen eventually WILL happen. Illogical or not.

                        M Offline
                        M Offline
                        musefan
                        wrote on last edited by
                        #12

                        I didn't say it was unlikely, I said it was possible that it could never happen. That does not mean sometimes it could happen, it means other code could decide that it will never happen.

                        oggenok wrote:

                        things that CAN happen eventually WILL happen

                        ...and that is not certain if I wrote...

                        string s = "hello ";
                        s = s.Trim();

                        ...would you correct with...

                        string s= "hello ";
                        if(!string.IsNullOrEmpty(s))
                        s = s.Trim();

                        EDIT: Not understanding my message is not justification to vote it down.

                        Illogical thoughts make me ill

                        modified on Tuesday, March 1, 2011 10:22 AM

                        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