C # string details,
The most commonly used method is string, but sometimes some problems are easy to make mistakes without careful consideration. Today I will summarize the usage of string.
1. string is a reference type. We usually compare the string object to compare the object value rather than the object itself.
For example:
string strA="abcde";string strB="abc";string strC="de";Console.WriteLine(strA == (strB+strC));//trueConsole.WriteLine((object)strA == (object)(strB+strC));//false
Because the string content is the same, but not the same instance is referenced
2. The string object cannot be modified.
string strA="abcde";strA="aaaaa";
On the surface, it seems that the contents of strA have been modified. In fact, "abcde" is not modified, but a new object "aaaaa" is created, and then the reference of this object is assigned to strA, finally, "abcde" will be recycled as garbage.
3. Create a string
Direct Value assignment: string strA = "abcde"; // create a string object whose content is abcde, and then assign the reference of this object to strA
Construction: char [] arr = {'A', 'B', 'C', 'D', 'E '};
String strA = new string (arr); // only one type is listed here.
Note: There is no String str = new String ("abcde"); in this way, string is the alias of String in. NET Framework.
3. string parameter transfer
String is a reference type. We try to change this value in a function.
static void Main(string[] args){ string strA = "abcde"; Deal(strA); Console.WriteLine(strA); Console.ReadLine();}static void Deal(string str){ str = str.Substring(0, 2);}
Result: abcde
Cause: When a parameter of the reference type is passed through a value, it is possible to change the data pointed to by the reference, such as the value of a certain type of member. However, the value of the reference itself cannot be changed. This problem can be solved by passing parameters through the ref keyword.
static void Main(string[] args){ string strA = "abcde"; Deal(strA); Console.WriteLine(ref strA); Console.ReadLine();}static void Deal(ref string str){ str = str.Substring(0, 2);}
Result: AB
In this case, the reference is passed, not the copy.
4. null string and null String
Null String: no memory is allocated. The null String is allocated with memory, but there is no data in the memory.
Static void Main (string [] args) {string strA = "1"; string strB = string. empty; string strC = null; Console. writeLine (int. parse (strA); // correct Console. writeLine (int. parse (strB); // The format of the input string is incorrect Console. writeLine (strC. toString (); // the instance where the object is not referenced. Console. ReadLine ();}
Whether the built-in method string is null or null:
IsNullOrEmpty is equivalent to if (str = null | str. Equals (String. Empty ))
IsNullOrWhiteSpace is equivalent to if (str = null | str. Equals (String. Empty) | str. Trim (). Equals (String. Empty ))
5. StringBuilder
string strA="abc"for(int i=0;i<10000;i++){ strA+="abc";}Consolse.WriteLine(strA);
Although the Code will appear to append new characters to an existing String named strA using String concatenation, it will actually create a new String object for each concatenation operation. This greatly reduces performance. You can use the StringBuilder class to change the String value multiple times instead of the String class. The StringBuilder object is variable. When you append or delete a substring in a String, no new object is created, instead, modify the original object. After modifying the value of the StringBuilder object, you can call its StringBuilder. ToString method to convert it to a string.
StringBuilder strA=new StringBuilder();for(int i=0;i<10000;i++){strA.Append("abc");}Consolse.WriteLine(strA.ToString());
Symbol in C Language <Yes
Left shift operator (<)
Removes all the binary bits of an operation object from the left and adds 0 to the right ).
For example, a = a <2 shifts the binary bits of a two places to the left and complements 0 to the right,
Move 1 to the left and then a = a * 2;
If the left shift does not include 1 in the Discard high position, then shifts one bit left, which is equivalent to multiplying the number by 2.
Shift right operator (>)
Shifts all the binary bits of a number to several places to the right, and adds 0 to the left of the positive number, 1 to the left of the negative number, and discards the right of the negative number.
The operand shifts one digit to the right, which is equivalent to dividing the number by 2.
For example, a = a> 2 shifts the binary bit of a two places to the right,
0 or 1 to see whether the number is positive or negative.
Symbol in C Language <Yes
Left shift operator (<)
Removes all the binary bits of an operation object from the left and adds 0 to the right ).
For example, a = a <2 shifts the binary bits of a two places to the left and complements 0 to the right,
Move 1 to the left and then a = a * 2;
If the left shift does not include 1 in the Discard high position, then shifts one bit left, which is equivalent to multiplying the number by 2.
Shift right operator (>)
Shifts all the binary bits of a number to several places to the right, and adds 0 to the left of the positive number, 1 to the left of the negative number, and discards the right of the negative number.
The operand shifts one digit to the right, which is equivalent to dividing the number by 2.
For example, a = a> 2 shifts the binary bit of a two places to the right,
0 or 1 to see whether the number is positive or negative.