C #

Source: Internet
Author: User

First, identify the differences between "", null and string. Empty:

String. Empty: No storage space is allocated.

"": Allocate a bucket with an empty length, "" and string. empty, both of which indicate null strings. A null string is a special string, but the value of this string is null and has an accurate point in memory.

String. Empty is equivalent to "", which is generally used for string initialization. For example, string a = string. empty. String. Empty is the same. That is, if string test1 = ""; then you can use if (test1 = "") or if (test1 = string. Empty) for judgment. The above two statements have the same effect.

Null: the null keyword is a text value that does not reference any object's null reference. Null is the default value of the reference type variable. Then, only the referenced variable can be null. If int I = NULL, it is not possible because Int Is of the value type.

String. empty and null, both of which indicate null strings, string str1 = string. empty. After this definition, str1 is an empty string, and the empty string is a special string, except that the value of this string is null and has an accurate point in the memory, string str2 = NULL. After this definition, a reference to the string class is defined. str2 does not point to any place. If it is not instantiated before use, an error is returned. So the followingCodeTest3.length = 0 is incorrect.

Judge an empty string:

String Test1 = "" ;
String Test2 = String . Empty;
String Test3 = Null ;
Response. Write ( " Test1 = \"\" " + " " );
Response. Write ( " Test2 = string. Empty " " </BR> " );
Response. Write ( " Test3 = NULL " + " </BR> " );
If (Test1 = "" )
Response. Write ( " (Test1 = \ "\") is: True " + " </BR> " );
If (Test2 = String . Empty)
Response. Write ( " (Test2 = string. Empty) is: True " + " </BR> " );

If (Test1 = String . Empty)
Response. Write ( " (Test1 = string. Empty) is: True " + " </BR> " );
If (Test2 = "" )
Response. Write ( " (Test2 = \ "\") is: True " + " </BR> " );

If(Test1=Test2)
Response. Write ("(Test1 = Test2) is: True" + "</BR>");

If (test3 = null )
response. write ( " (test3 = NULL) is: True " + "
" );

If (test1 ! = null )
response. write ( " (test1! = NULL) is: True " + "
" );

If (Test2 ! = null )
response. write ( " (Test2! = NULL) is: True " + "
" );

If(Test1.length=0)
Response. Write ("(Test1.length = 0) is: True" + "</BR>");

If (Test2.length = 0 )
Response. Write ( " (Test2.length = 0) is: True " + " </BR> " );
// If (test3.length = 0) // Error, null cannot be determined as null by length.
If ( String . Isnullorempty (test1 ))

Response. Write ("(String. isnullorempty (test1) is: True" + "</BR>");
If(String. Isnullorempty (Test2 ))

response. write ( " (string. isnullorempty (Test2) is: True " + "
" );
If ( string . isnullorempty (test3)

Response. Write ("(String. isnullorempty (test3) is: True" + "</BR>");
Output:

Test1 = ""
Test2 = String . Empty
Test3 = Null
(Test1 = "" ) Is : True
(Test2 = String . Empty) Is : True
(Test1 = String . Empty) Is : True
(Test2 = "" ) Is : True
(Test1 = Test2) Is : True
(Test3 = Null ) Is : True
(Test1 ! = Null ) Is : True
(Test2 ! = Null ) Is : True
(Test1.length = 0 ) Is : True
(Test2.length = 0 ) Is : True
( String . Isnullorempty (test1 )) Is : True
( String . Isnullorempty (Test2 )) Is : True
( String . Isnullorempty (test3 )) Is : True

Therefore, the most common method to judge whether a string is null is isnullorempty (), whether it is "", String. Empty or null. If the string Initialization is null, test3.length = 0 cannot be used for determination. For "", and string. Empty, use S. Length = 0, S = string. Empty and S = "". performance issues are not discussed here.

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.