Abstract: string is the most frequently used type in programming. A string represents a constant set of character sequences. The string type is directly inherited from the object, so it is a reference type, that is, there is no string on the thread stack (the type directly inherited from the object must be a reference type, because all value types are inherited from system. valuetype. It is worth noting that system. valuetype is a reference type ).
String is the most frequently used type in programming. A string represents a constant set of character sequences. The string type is directly inherited from the object, so it is a reference type, that is, there is no string on the thread stack (the type directly inherited from the object must be a reference type, because all value types are inherited from system. valuetype. It is worth noting that system. valuetype is a reference type ).
1. Create a string
C # Think of string as a primitive type, that is, the compiler may use text constants in the source code to directly express the string. The compiler stores these text constant strings in the metadata of the managed module. Create a string in C.
You cannot use the new operator to create a string.
Class Program
{
Static void main (string [] ARGs)
{
String Hap = new string ("heaiping ");
}
}
The above code may cause a compilation error.
Instead, use the following simplified syntax
Class Program
{
Static void main (string [] ARGs)
{
String Hap = "heaiping ";
}
}
For more information, see the following code.
Code namespace stringstudy
{
Class Program
{
Static void main (string [] ARGs)
{
String S = "heaiping"; // do not use new
Someclass SC = new someclass (); // use new
}
}
Class someclass
{
}
}
Compile the above Code to view the Il code generated by ildasm as follows.
Code. Method private hidebysig static void main (string [] ARGs) cel managed
{
. Entrypoint
// Code size 14 (0xe)
. Maxstack 1
. Locals Init ([0] string s,
[1] class stringstudy. someclass SC)
Il_0000: NOP
Il_0001: ldstr "heaiping" // create a string here
Il_0006: stloc.0
Il_0007: newobj instance void stringstudy. someclass:. ctor () // create someclass here
Il_000c: stloc.1
Il_000d: Ret
} // End of method program: Main
As shown above, C # uses newobj as the Il command for the object instance and then calls its constructor. For string, it uses a special command ldstr (whether it is loadstring or not ), this command obtains text constants from metadata to construct string objects. This indicates that CLR has a more efficient and special string construction method.
The string type is also constructed by using new instead of text metadata. It can accept parameters such as char *, which is provided to host C ++, this will not be studied ~~~~~~~
See the following code.
Class Program
{
Static void main (string [] ARGs)
{
String S = "heaiping ";
S = S + "" + "soft ";
}
}
The above Code uses the + operator to connect strings. We do not recommend using the + operator in the book, because it will create multiple strings on the hosting stack for the garbage collector to be executed, rather than simply attaching them to the back. Use System. Text. stringbuilder (next learning );
There is also a special declaration that allows the compiler to consider the characters between quotation marks as part of a string.
Code class Program
{
Static void main (string [] ARGs)
{
// Filea and fileb are the same
String filea = "C: // windows // Temp ";
String fileb = @ "C:/Windows/Temp ";
}
}
@ Symbol will speed up the compiler. This character string is a literal string, telling the compiler to regard the backslash as a character rather than an escape character.
Ii. Constant character of strings
The uniqueness of a string, that is, once a string is created, it cannot become longer, shorter, or change any of the characters.
Iii. String comparison
List
Member |
Member type |
Description |
Compare |
Static Method |
Sort and compare strings to control the language and culture, and determine whether to consider the case sensitivity. |
Compareto |
Instance method |
Sort and compare strings internally using the language culture of the current thread |
Startswith/endswith |
Instance method |
Whether to start or end with a specified string. It is case sensitive and uses the language culture of the current thread. |
Compareordinal |
Static Method |
Compare by character set, regardless of language culture, case sensitive, fast |
Eauals |
Static/instance |
Static Methods Compare character sets. The instance method calls compareordinal internally. The static method first checks whether two references point to the same object. If yes, the character set is no longer compared. |
Gethashcode |
Instance method |
Returns the hash code. |
In addition to the above, the string type also carries the = and! = Operator, which calls the static equals method of string internally.
The preceding comparisons are described as follows:
- Compareoridinal is used to determine whether a string is equal, because it only compares whether character sets are equal and is faster.
- If it is a logical comparison that is presented to the user, what is logical comparison? Although some strings have different character sets, they are logically equal. The compare method should be used, compare uses a sorting table for specific languages and cultures.
When comparing strings, the language culture has a great impact on them. The compare method first obtains the currentculture related to the calling thread internally, and then reads the compareinfo attribute of currentculture. Because the compareinfo object encapsulates a character comparison table associated with the language and culture, each culture has only one compareinfo object.
Iv. String resident
The character bed comparison is a waste of performance. CLR improves performance through the string interning mechanism. See the following code:
Code
Namespace stringstudy
{
Class Program
{
Static void main (string [] ARGs)
{
String S = "Hap ";
Console. writeline (object. referenceequals ("Hap", s ));
Someclass SC = new someclass ();
Console. writeline (object. referenceequals (New someclass (), SC ));
}
}
Class someclass
{
}
}
According to the characteristics of the reference type, both the above two outputs should be false, because different references are compared,However, the output of console. writeline (object. referenceequals ("Hap", S) is true. Why is string not a reference type ??String is of course a reference type, and the above performance is determined by CLR's special processing of string:
When CLR is initialized, it creates an internal hash with the key as a string and the value as a reference to the string object in the managed heap. At the beginning, the table is empty. When the JIT compiler compiles a method, it searches for each text constant string in the hash. For the above Code about string, the compiler will first look for the "Hap" string, and because it is not found (not found for the first time ), it constructs a New String object (pointing to the HAP) on the managed heap ), then, add the hybriddb string and reference pointing to the object to the hashed list (the hash list contains the value with the key of "Hap"). Then, the JIT compiler searches for the second hybriddb string in the compiler, of course, the hash list already exists, so no operation is executed and the code starts to be executed.
When the code is executed, it will find a reference to the needing to be referenced by the hap string in the first line. Therefore, CLR searches for the hap in its internal hash and finds it, so that reference to the previously created String object is saved in the variable S. When you execute the command again, the CLR will search for the HA again in its internal hash and will still find it. In this way, references pointing to the same string object will be passed to the object's referenceequals as the first parameter. Of course, it is the same reference as the second parameter, and the result is true.
Continue with the following code:
Code namespace stringstudy
{
Class Program
{
Static void main (string [] ARGs)
{
String S1 = "Hap ";
String S2 = "H ";
String S3 = S2 + "AP ";
String S4 = "H" + "AP ";
Console. writeline (object. referenceequals (S1, S3); // false
Console. writeline (s1.equals (S3); // true
Console. writeline (object. referenceequals (S1, S4); // true
}
}
}
This time, the output is a little strange. What is the problem? Originally, when a string reference method is compiled by JIT, all text constants embedded in the source code will be added to the hash list. String referenced by S2 (h) is connected with a text constant string (AP. The result is a newly constructed string object that is referenced by S3 in the managed heap. The dynamically created string contains a hap, but it is not added to the CLR hash, instead, we recommend that you reference all referenceequals to return fasle. Equals is called because they have the same character set.
For S4 = "H" + "AP", this is because the Il command concatenates two text constant strings into a text constant string, and all outputs are true.
The referenceequals method does not need to compare characters one by one, but only compares references. The efficiency is significantly higher than that of equals. If we compare all strings in the program with references rather than character sets, then the system performance will be greatly improved. If there is a way to change a dynamic string containing the same character set to a string object in the managed heap, the application requires fewer objects, which can improve system performance. Very lucky. The string type improves the two static methods to achieve this.
Public static string intern (string Str );
Public static string isinterned (string Str );
The first method intern accepts a string parameter and searches for it in the CLR hash. If this string can be found, intern returns an existing reference. If no value is found, the string will be added to the hash list, and the reference of intern will be returned. If the reference program no longer saves the string reference passed as the parameter, the garbage collector will recycle it. Use intern to re-Modify the above program:
Code namespace stringstudy
{
Class Program
{
Static void main (string [] ARGs)
{
String S1 = "Hap ";
String S2 = "H ";
String S3 = S2 + "AP ";
S3 = string. Intern (S3 );
Console. writeline (object. referenceequals (S1, S3); // true
Console. writeline (s1.equals (S3); // true
}
}
}
The output is all true, which is amazing. Because intern also requires execution time, it is recommended that you use the string resident technology only when you need to compare the same string multiple times. Otherwise, the loss will not be worth the candle.
Note that the garbage collector does not release the string objects referenced in the CLR internal hash. Only when the application domain no longer applies these string objects will they be released.
The difference between the isinterned method and the intern method is that if no value is found in the hash list, null is returned instead of being created.
Code namespace stringstudy
{
Class Program
{
Static void main (string [] ARGs)
{
String S1 = "heaiping ";
String S2 = "H ";
String S3 = S2 + "AP ";
S3 = string. isinterned (S3 );
If (S3 = NULL)
{
Console. writeline (0 );
}
Else
{
Console. writeline (1 );
}
}
}
}
The above program outputs 0, because in the current program, there is only heaiping in the hash list and there is no ha, so S3 becomes null at last.
String also has some members such as length, indexof... copy... and so on... I'm tired.
By the way, I sent a group discussion http://home.cnblogs.com/group/topic/38270.html about string.
The original article is as follows:
Class class1
{
Static void strchange (string Str)
{
STR = "Hellow ";
}
Static void main ()
{
String STR = "123"; // declare a string
Strchange (STR); // CALL THE METHOD
Console. writeline (STR); // output string
}
}
The output result is 123"
Is string a value type or a reference type?
If it is a value type, the result is still said. But I remember that string is a reference type... is it wrong ??
If it is a reference type, the output result should be: "Hellow"
Why ?? Thank you for your help.
Now we understand that it is actually related to the CLR's hash list for the string,
String is a special reference type. If it is special, it should be on this hash.
C # Text Processing (string) Study Notes