February 7, 2005 21:56:16 Strings
A string is a instance of System.String (or simplified to lowercase String) class in C #, which represents a constant character sequence. The syntax created by the string is very simple: string s = "c # sharp XP", that is, the contents of the content as the string "C # Sharp XP" on the hosted stack, and S is only a reference handle to the memory area.
Look at the following code: String S1 = "Hello, World!"; String S2 = S1;
At this time, S1 and S2 are directed to the same block containing "Hello, World!".
C # provides two escape expressions for strings. The first type of escape character in traditional C / C is the same, that is, the expression of a counter-slope "/" plus atrical character. If "/ t" indicates the Tab key, "/ R" is represented by the carriage return, "/ n" means a wrap, etc. To indicate a file path under the C disk "My Document": string mypath = "c: // Documents and settings // cornfield // my documents // myfile.cs", where double slash is essential to single oblique line. The second representation method can be added to a "@" symbol before the string, so that the string can be expressed in the normal character sequence, as the above file path can be expressed like this: String mypath = @ "c : / Documents and settings / cornfield / my documents / myfile.cs ", the same as the previous expression. The second representation is useful in the case where we express a long string containing special characters, it can even express special characters such as enter-in-law, wrap, etc.
You can get a single character in the string (16-bit Unicode encoding) in the string - this and the string array in C / C is essentially different. The following code demonstrates this: string s = "c # sharp xp"; for (int index = 0; index Due to the constant string constant, we can't do a change in S [index] = 'p', which is guaranteed by system.string read-only index PUBLIC CHAR this [int index] {get;}. As an instance of the System.String type, the system provides a rich and diverse operation for strings, and only a few special methods are paired. The String class implements the iCloneable interface, but the string.clone method is not a newly created String instance, but only returns a reference to the same reference handle, that is, "String S2 = S1.Clone ()" and front "String S2 = S1" effect is the same. It is necessary to return one and parameter content, but the different strings of the reference hand can use a static string.copy method. Example method system.equal and class operator "Public Static Bool Operator == (String A, String B);" Compare is the contents of the two strings, rather than the censors. The following code example, the above-described method of the above method: use system; class test {spread s1 = "hello, world!"; String s2 = s1.clone (). TOSTRING (); / / Cloning, handle, etc. String S3 = string.copy (S1); // copy, handle is not equal, content equal console.writeLine (S1 == S2); // true, content equal console.writeline (S1 = = S3); // true, content equal console.writeline ((object) S1 == (Object) S2); // true, handle reference equal console.writeLine ((Object) S1 == (Object) S3); / / False, handle reference}}} string constant The constant meaning of the string means that it cannot be changed once the value of the string is created (System.String instance). Look at the following typical code: String S1 = "Hello,"; STRING S2 = "World!"; S1 = S2; After the above statement is executed, S1 is "Hello, World!", S2 is "World!". However, the original value "Hello" did not disappear, and "Hello" still has memory space, but now I can't be quoted by us, it can only wait for .NET's automatic garbage collector to recycle its resources. This causes memory leak in the previous C , which means that we will have "Hello, World!" (S1 reference handle points to the value), "World!" (S2 reference handle pointing Value), "Hello," (without a reference handle pointing this value) a total of three memory areas of the string. The character string value is also manifested on the parameter transmission as a string type, we look at the following example: use system; class test {public static void main () {string p = "hello,"; mymethod (p); console .Writeline (p); // output "hello,"} public static void mymethod (string p) {p = "world!"; Console.writeLine (P); // Output hello, world!}} Although the String type is a reference type in C #, due to the constant nature of the string, the method MyMethod does not change the reference handle of the passed P. (if you want to change, you need to use the REF keyword modification parameters), and this reference The handle itself points to the expression string "Hello," memory area has not changed, and naturally the calling string P through the MyMethod method will not change. Understanding the characteristics of the string value is very important to us in the C #, such as the System.String class, many ways do not change the string itself involved in the operation, but create a new value, just put the original The string is left to the auto garbage collector. If the string is frequent in the C # program, it is possible to cause a large number of "creation / discarding" actions, which must cause the system to make the system, and we should use the StringBuilder class. StringBuilder is located under System.Text Namespace, which is not a string, but it provides a rich operation for a variable string: insert, addition, replacement, deletion, etc., at the same time due to its operation, Therefore, there is no constant string system.String frequent "creation / discard" consumption, which can be used to create a StringBuilder class instance containing the Value string value using the constructor "public stringbuilder;", you can also use StringBuilder. Example method TOSTRING is very convenient to get content of type String string. In contrast to the System.String class, the StringBuilder class changes the StringBuilder class instance that participates in the operation, does not create a new value! As a general principle, if only the contents of the string represent or create a new string, it is often possible to use system.string method, but if it involves a large number of string contents, it is very It is necessary to use some related methods of the StringBuilder class, and then call the TSTRING method to get the string we need. 1. Extract the substring StringBuilder class from the string does not support substrings, so you must use the String class to extract. String mystring = "my name is ynn."; // displays "name is ynn." console.writeline (mystring.substring (3)); // displays "Ynn" console.writeline (11,3) );