We introduced two basic features of string in the first two sections. If you think you have fully understood string, let's take a look at the two sections 3rd and 4.
3. Interesting comparison operations
In sections 1 and 2, we have introduced the uniqueness and residency of strings. If this fellow student feels that he has mastered the above content, check your learning achievements in section 3!
The following 10 simpleCodeCompare the value with the address reference to explain the content mentioned in the previous two sections. You can also use the code to check your understanding of the string.
Code 1:
String A = "str_1 ";
String B = "str_1 ";
Response. Write (A. Equals (B ));
Response. Write (referenceequals (a, B ));
Output: true (equals compares the value of a string object)
True (referenceequals compares the references of string objects. Because of the string resident mechanism, the references of A and B are the same)
Code 2:
String A = "str_1str_2 ";
String B = "str_1 ";
String c = "str_2 ";
String d = B + C;
Response. Write (A. Equals (d ));
Response. Write (referenceequals (a, d ));
Output: true (equals compares the value of a string object)
False (referenceequals compares the reference of a string object. The string resident mechanism is invalid because the value of variable D is the result of variable connection)
Code 3:
String A = "str_1str_2 ";
String B = "str_1" + "str_2 ";
Response. Write (A. Equals (B ));
Response. Write (referenceequals (a, B ));
Output: true (equals compares the value of a string object)
True (referenceequals compares the reference of a string object. Because the value of variable B is the result of a constant connection, the string resident mechanism is effective. If the value of variable B is obtained by "constant + variable", the string resident is invalid)
Code 4:
String A = "str_1 ";
String B = string. Copy ();
Response. Write (A. Equals (B ));
Response. Write (referenceequals (a, B ));
Output: true (equals compares the value of a string object)
False (referenceequals compares the reference of a string object, and the copy operation produces a New String object)
Code 5:
String A = "str_1 ";
String B = string. Copy ();
B = string. Intern (B );
Response. Write (A. Equals (B ));
Response. Write (referenceequals (a, B ));
Output: true (equals compares the value of a string object)
True (referenceequals compares the references of string objects, and string. Intern implements string resident)
Code 6:
String A = "str_1 ";
String B = string. Copy ();
String c = "str_1 ";
Response. Write (object) A = (object) B );
Response. Write (object) A = (object) C );
Output: false ("=" compares the referenced address when both sides are of the reference type, so a and B are different references)
True (referenceequals compares the references of string objects. A and C share the same references due to the string resident mechanism)
Code 7:
String A = "str_1 ";
String c = "str_1 ";
Response. Write (A = C );
Output: True
As we mentioned earlier, "=" compares the referenced address when both sides are of the reference type. If it is a value type, you need to compare the referenced value. String is the reference type. Does the code above compare the addresses and values of variables A and C? The answer is: Compare the address and value! Because "=" has been overloaded as "equals" during string type comparison, although you are comparing two reference types with "=, but they are actually comparing their addresses and values with "equals! (Compare the address first, and compare the value after the address is not equal)
Code 8:
String A = "";
String B = new string ('A', 1 );
Response. Write (A. Equals (B ));
Response. Write (referenceequals (a, B ));
Output: true (equals comparison value, A and B are the same)
False (referenceequals compares the references of string objects)
Code 9:
String A = "";
String B = new string ('A', 1 );
Response. Write (A. Equals (string. Intern (B )));
Response. Write (referenceequals (A, String. Intern (B )));
Output: true (equals comparison value, regardless of whether intern is the same)
True (referenceequals compares the references of string objects, and intern has placed B in the string pool)
Code 10:
String A = "str ";
String B = "str_2". substring (0, 3 );
Response. Write (A. Equals (B ));
Response. Write (referenceequals (a, B ));
Output: true (equals comparison value, A and C values are the same)
False (referenceequals compares the reference of a string object, and the substring operation generates a new String object)
This code segment generates three string objects. What are the three? If you don't understand, read it again from the beginning!
Iv. yihai jiebei
This section mainly introduces some common string problems.
1. difference between "string =" and "New stirng ()"
String test = "";
String test = new string ('A', 1 );
The effects of the above two lines of code are the same. The difference is that the loading time for "A" is different: the "A" in the first line is a constant, it has already been placed in a place called a constant pool during the compilation period. The constant pool usually loads data identified during the compilation period, such as classes and interfaces; the second line is the string object generated by the CLR In the heap at runtime with the value "A", so the latter does not reside.
2. Differences between string and string
The name of string is system. string, string and system. string generates the same code: (PS: Long and system. int64, float, and system. single and so on)
C # code:
String str_test = "test ";
System. String str_test = "test ";
Il code:
// Code size 14 (0xe)
. Maxstack 1
. Locals Init ([0] string str_test,
[1] string str_test)
Il_0000: NOP
Il_0001: ldstr "test"
Il_0006: stloc.0
Il_0007: ldstr "test"
Il_000c: stloc.1
Il_000d: Ret
Therefore, the difference between the two lies not in the underlying layer, but in the fact that string is a primitive type similar to int; system. String is the basic type of the framework class library (FCL), and there is a direct relationship between the two.
3. stringbuilder
Stringbuilder provides an efficient method to create strings. Strings represented by stringbuilder are variable (non-constant). When you need to use "+" to connect string variables in multiple places, we recommend that you use stringbuilder to complete the task and call its tostring () method to output the task. After the stringbuilder tostring () method is called, stringbuilder returns a string field reference maintained internally. If you modify stringbuilder again, it creates a new string, at this time, the new string is modified, and the original returned string will not be changed.
Stringbuilder has two important internal fields that you need to know:
M_maxcapacity: the maximum capacity of stringbuilder. It specifies the maximum number of characters that can be placed in m_stringvalue. The default value is int32.maxvalue. M_maxcapacity cannot be changed once it is specified.
M_stringvalue: A character array string maintained by stringbuilder. It can be understood as a string. This field is returned by the tostring () method rewritten by stringbuilder.
At this point, you have mastered most of the secrets of string. How about it? You should have a new understanding of string!
I amAicken)Please keep an eye on my next articleArticle.