locked
String Functions RRS feed

  • Question

  • User179805481 posted

    Hi all,

    What is the difference between the following string comparisons??

    -> String.IsNullOrEmpty(strVaue) 

    and

    -> strValue == string.Empty

    and

    -> strValue == ""

     

    Friday, March 12, 2010 7:13 AM

Answers

  • User-1360095595 posted

    IsNullOrEMpty will check if the string is, well, null or empty.  The other two only check if the string is empty. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, March 12, 2010 7:32 AM
  • User1807801129 posted

    To check for both NULL and String.Empty, you need to write following condition:

    strValue=="" || strValue==null

    instead of this you can directly use the function IsNullOrEmpty. If gives better readablility and understanding. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, March 12, 2010 8:14 AM
  • User-1812063630 posted

    hi,

    there is no such big advantages.
    but using string.IsNullOrEmpty
    you can use this make your code tighter and less wordy.



    Please remember to click “Mark as Answer” on the post that helps you.

    This can be beneficial to other community members reading the thread.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, March 12, 2010 8:22 AM

All replies

  • User-1360095595 posted

    IsNullOrEMpty will check if the string is, well, null or empty.  The other two only check if the string is empty. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, March 12, 2010 7:32 AM
  • User162050988 posted

    String.Empty and str=="" are same

    but String.IsNullOrEmpty(str) is a function that returns true if str is null or empty

    Friday, March 12, 2010 7:34 AM
  • User-1812063630 posted

    1) String.IsNullOrEmpty(strVaue)

    It is a function which returns a true or false value.
    passed string into this function if it is empty then it will return true.
    otherwise it will return as false


    2) strValue == string.Empty
    and
    3) strValue == ""

    the above two condition also works as same as first function diffrence is only
    that these are mannual condition used in traditional if statement.

    But it is always recommended to use IsNullOrEmpty function to check string is contained some data or not.


    Please remember to click “Mark as Answer” on the post that helps you.

    This can be beneficial to other community members reading the thread.


    Friday, March 12, 2010 7:55 AM
  • User179805481 posted

    Y is it recommended so?

    Does this have any advantages compared to the other two forms? 

    Friday, March 12, 2010 8:09 AM
  • User1807801129 posted

    To check for both NULL and String.Empty, you need to write following condition:

    strValue=="" || strValue==null

    instead of this you can directly use the function IsNullOrEmpty. If gives better readablility and understanding. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, March 12, 2010 8:14 AM
  • User-1812063630 posted

    hi,

    there is no such big advantages.
    but using string.IsNullOrEmpty
    you can use this make your code tighter and less wordy.



    Please remember to click “Mark as Answer” on the post that helps you.

    This can be beneficial to other community members reading the thread.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, March 12, 2010 8:22 AM