C # string null (IsNullOrEmpty)

Source: Internet
Author: User

String str

1:string. IsNullOrEmpty = = Str This method is my favorite, it not only can judge "empty string variable", but also can judge "value is empty string variable", and also can make the code simple and beautiful. The efficiency of judgment is also not low.

2:str. Length = = 0 This way, I do not like to use, not recommended. Looking at the Internet and your own actual tests can prove that this is the most efficient way to do this, but to use it you have to make sure that the string is not NULL, and if it is null, the exception is reported.

3.str = = String. Empty or str = = "" These two ways, I also do not recommend to use, he can only judge "value is empty string" of the string variable, and efficiency is low

4.STR = = NULL This way I am not recommended, reason and 31 kind.

The following is from Microsoft Official website:

String.IsNullOrEmpty method

indicates that the specified string is null or isEmptystring.

public static bool IsNullOrEmpty (string value)
Parameters
    • Value

    • Type: System. String
      The string to test.

return value

Type: System. Boolean
trueif the value parameter is null or an empty string (""); otherwise false.

isnullorempty is an easy way to test whether a String is null at the same time or whether its value is empty.  it is equivalent to the following code:

result = S = = NULL | | s = = String.Empty;

can useIsnullorwhitespacemethod to test whether a string null , whose value isString. Empty, or contains only white space characters.

Using system;class sample {    public static void main ()      {    string s1 =  "ABCD";     string s2 =  "";    string s3 = null;     console.writeline ("string s1 {0}.",  test (S1));     console.writeline (" String s2 {0}. ",  test (S2));     console.writeline (" string s3 {0}. " ,  test (S3));     }    public static string test ( String s)     {    if  (String.IsNullOrEmpty (s))           return  "Is null or empty";     else        return string.format ("(\" {0}\ ")  is  Neither null nor&nBsp;empty ",  s);     }} 



C # string null (IsNullOrEmpty)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.