1 stringstr1="Test"; 2 stringSTR2 ="Test"; 3 if(STR1==STR2)//The first way of judging4 { 5 //The second way of judging6 intRESULT1 =Str1.compareto (STR2); 7Console.WriteLine (RESULT1);//Output result1=08 9 //The Third Way of judgingTen intResult2=String.Compare (str1, str2); OneConsole.WriteLine (RESULT2);//Output result2=0 A - //the following two ways to be aware that str1 and str2 are not NULL cases - the //The Fourth way of judging - BOOLresult3=str1. Equals (STR2); -Console.WriteLine (RESULT3);//Output Result3=true - + //The fifth way of judging - BOOLRESULT4 =string.equals (STR1,STR2); +Console.WriteLine (RESULT4);//Output Result4=true A}
Note: String.Compare The meaning of the comparison result:
Value |
Meaning |
less than 0 |
x is less than y. or x is a null reference (Nothing in Visual Basic). |
0 |
x equals y. |
greater than 0 |
x greater than y. or y is a null reference (Nothing in Visual Basic). |
String. The meaning of the equals comparison result is:
Value |
Meaning |
True |
x equals y. |
False |
x does not equal Y. |
Char a = null, or char a = ' ' is all wrong to define a null value. The following methods are summarized: the values are all 0.
1 stringSTR1 =NULL;2Console.WriteLine ("str1 = {0}", str1);//show null values and "" similar3 /*4 * The variable value of char below is all 05 */6 7 CharTest =' /';8 intNUM1 =Convert.tochar (test);9Console.WriteLine ("NUM1 = {0}", NUM1);Ten One CharTest1 =Char. MinValue; A intnum2 =Convert.tochar (test1); -Console.WriteLine ("num2 = {0}", num2); - the CharTest2 = (Char)0; - intNUM3 =Convert.tochar (test2); -Console.WriteLine ("num3 = {0}", num3); - + CharTest3 = Convert.tochar (0); - intNUM4 =Convert.tochar (test3); +Console.WriteLine ("num4 = {0}", NUM4); A at CharTest4 = ((Char?)NULL). GetValueOrDefault (); - intNUM5 =Convert.tochar (test4); -Console.WriteLine ("NUM5 = {0}", NUM5);//the values are all 0 . - -Console.readkey ();
C # Methods for judging whether two strings are equal, and the char assignment null method.