In C # built-in types, strings are unique reference types with immutability and tightness.
Non-denaturing refers to a string once created, it cannot be changed, STR1=STR1, actually creates a new Str1 string object in the heap, and returns a reference to the object.
Sealing refers to a string that cannot be inherited.
Immutability and sealing performance guarantees a more efficient and secure string type.
String manipulation
1. Formatting strings
String.Format
string str1 = " puguo 2530 string Strformat = " {0,20} ; // string formatting takes 20 characters string strresult=. Format (STRFORMAT,STR1); Console.WriteLine ( string. Format ({0},{1}) =[{2}]\n , Strformat,str1,strresult);
Standard Numeric formatted string |
Meaning |
Instance |
C or C |
Currency |
String. Format ("{0:c}", 10) =¥10.00 |
D or D |
Fill-In |
String. Format ("{0:d3},10") =010 |
E or E |
Index |
String. Format ("{0:e3},10") =1.000e+001 |
F or F |
Fixed point |
String. Format ("{0:f3},10") =10.000 |
G or G |
Conventional |
String. Format ("{0:g3},10") =10 |
N or N |
thousand-minute character |
String. Format ("{0:n},10000") =10,000.00 |
P or P |
Percent Sign |
String. Format ("{0:p},1") =100% |
X or X |
16 System |
String. Format ("{0:x}", 1900) =96c |
Custom Numeric formatting strings |
Meaning |
0 |
0 placeholder |
# |
Digit placeholder |
. |
Decimal point |
, |
Thousands separator |
; |
Segment Separator |
% |
Percent symbol |
2. Comparing strings
(1), String.Compare (STR1,STR2)
STR1<STR2--->-1
STR1==STR2--->0
STR1>STR2--->1
(2), string.compareordinal (STR1,STR2); Unlike (1), which is not related to localization
STR1<STR2--->-1
STR1==STR2--->0
STR1>STR2--->1
(3), Str1.compareto (STR2);
STR1<STR2--->-1
STR1==STR2--->0
STR1>STR2--->1
STR2 is null--->1 "" greater than null
(4), String. Equals (str1, str2) <==> str1==str2
STR1==STR2--->0
str1! =STR2--->!0
(5), str1. Equals (STR2)
STR1==STR2--->0
str1! =STR2--->!0
(6), STR1==STR2
Summary: Static methods are more efficient than instance methods because static methods are loaded first than instance methods. Overall, (1) (2) ratio (3) Good, (4) than (5) good
3. String connection
(1), STR1+STR2
(2), String. Concat (STR1,STR2)
4. Copying strings
(1), str2=string. Copy (STR1);
Copy creates a new instance string object, and Str2,str1 does not point to the same string reference.
(2), STR2=STR1
Copy directly, str2 and str1 point to the same string reference. The immutability of the string causes the reference to the string to be immutable, changing the string reference to the string referenced by the string.
(3), str2= (String) Str1.clone ()
Str1.clone () Returns the object type, which is coerced to string and is consistent with the (2) effect.
C # string manipulation (i)