C # Sharp experience 12th strings

Source: Internet
Author: User
Tags class operator

12th lecture string

String
??????? In C #, a string is an instance of the system. String (or simplified to lowercase string) class. It represents a constant character sequence. The syntax for creating a string is very simple: String S = "C # Sharp XP"; that is, the memory area where the content of the string "C # Sharp XP" is created on the hosting stack, S is only a reference handle pointing to the memory area. Take a look at the following code lines:
String S1 = "Hello, world! ";
String S2 = S1;
??????? In this case, both S1 and S2 point to the same segment containing "Hello, world! "Memory zone.
C # provides two escape expressions for strings. The first type is the same as the Escape Character Expression in traditional C/C ++, that is, the backslash (/) is used to add a specific character. For example, "/t" indicates the tab key, "/R" indicates the carriage return, and "/N" indicates the line feed. We want to indicate a file path under drive C's "My documents", string mypath = "C: // Documents and Settings // cornfield // my documents // myfile. CS ", where the double slash is escaped as a single slash. With the second representation method, we can add a "@" symbol before the string to express the string in a normal string sequence. The above file path can be expressed as follows: string mypath = @ "C:/Documents and Settings/cornfield/My Documents ents/myfile. CS. The second representation is useful when we express long strings containing special characters. It can even express special characters such as carriage return and line breaks that cannot be displayed as characters.
You can use the indexer to obtain a single character (16-bit Unicode encoding) in a string. This is essentially different from the string array in C/C ++. The following code demonstrates this:
String S = "C # Sharp XP ";
For (INT Index = 0; index ???? Console. writeline (s [Index]);
??????? Due to the constant character string, we cannot make changes like s [Index] = 'P', which is caused by system. string's read-only indexer public char this [int Index] {Get;} to ensure.
??????? As an instance of the system. string type, the system provides a wide range of operations for strings. Here we only analyze several special methods. The string class implements the icloneable interface, but the string. the clone method does not return a newly created string instance, but returns only a reference handle with the same parameters. That is to say, string S2 = s1.clone () has the same effect as the previous string S2 = S1. You need to return a string with the same content as the parameter, but different reference handles can use the static string. COPY method. The instance method system. Equal is compared with the class operator public static bool operator = (string a, string B). It compares whether the content of the two strings is equal, rather than the reference handle. The following code example shows the behavior of the above method:
Using system;
Class Test
{
???? Public static void main ()
???? {
???????? String S1 = "Hello, world! ";
???????? String S2 = s1.clone (). tostring (); // clone. The handle is equal and the content is equal.
???????? String S3 = string. Copy (S1); // copy, handle, and content are equal
????????
???????? Console. writeline (S1 = S2); // true, equal content
?????? Console. writeline (S1 = S3); // true, equal content
????????
???????? Console. writeline (object) S1 = (object) S2); // true, handle references are equal
???????? Console. writeline (object) S1 = (object) S3); // false, handle references
????}
}

Constant character string
The constant character string means that once the value of the string (system. String instance) is created, it cannot be changed. Let's look at the following typical code line:
String S1 = "hello ,";
String S2 = "world! ";
S1 + = S2;
??????? After the preceding statement is executed, S1 = "Hello, world !", S2 = "World !". But the original S1 value "hello," does not disappear. Specifically, "hello," still occupies the memory space, but it cannot be referenced by us now. It can only wait. net Automatic Garbage Collector to recycle its resources, which caused Memory leakage in the previous C ++. that is to say, after the preceding statement is executed, we will have "Hello, world!" (S1 refers to the value pointed to by the handle), "World !" (S2 refers to the value pointed to by the handle), "hello," (no reference handle points to this value) Three memory zones that store strings.
??????? The character with constant string values is also transmitted as a string type parameter. Let's look at the following example:
Using 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, our method mymethod does not change the reference handle of the passed P (if you want to change it, you need to use the ref keyword to modify the parameter), and the reference handle itself points to the expression string "hello, "The memory region has not changed. Naturally, the call String P through the mymethod method will not change.
Understanding constant character values is very important for us to be proficient in operating strings in C #, such as system. many methods of the string class do not actually change the strings involved in the operation. Instead, they create a new value and only throw the original string to the automatic garbage collector. If the C # program operates strings frequently, it is likely to cause a large number of "Create/discard" operations on the strings, which will inevitably cause a burden on the system. In this case, we should use the stringbuilder class. Stringbuilder is located in system. in the text namespace, its instance is not a string, but it provides a variety of operations for variable strings-insert, append, replace, delete, etc, at the same time, because its operations occur in the same memory zone, there is no constant string system. frequent "Create/discard" Consumption of strings. You can easily use the constringbuilder public stringbuilder (string value); To create stringbuilder class instances containing value string values, similarly, you can use the stringbuilder instance method tostring to conveniently obtain the string content of the string type. Unlike many methods of the system. string class, many methods of the stringbuilder class change the stringbuilder class instance involved in the operation and do not create new values!
As a general principle, if we only involve the representation of the string content or create a new string, we can use the system. String method. However, if you want to change a large number of string content, it is necessary to use some related methods of the stringbuilder class, and then call the tostring method to obtain the required string.

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.