String Common Operations
(1) obtain the string length <string>. length;
(2) convert a string to a bit code getbytes (<string>)
(3) stringbuilder sb = new stringbuilder (); sb. append (<string>) is recommended for string addition );
(4) truncation of a part of the string variable. substring (starting position, number of digits intercepted );
(5) check whether the specified position is a NULL Character Char. iswhitespace (STRING variable, number of digits );
(6) check whether the character is a punctuation character Char. ispunctuation ('characters ');
(7) convert characters into numbers and check the Code Point (INT) 'variable'
(8) convert the number into a character and check the character (char) Code represented by the Code.
(9) Clear the Space Variable before and after the string. Trim ()
(10) replacement string: string variable. Replace (original string, new string)
(11) three methods for deleting the last character of a string
Eg: String S = "1, 2, 3, 4, 5 ,";
A) S. substring (0, S. Length-1) // Delete the last comma
B) S. tostring (). rtrim (','); // delete the comma. The variable followed is a valid string.
C) S. trimend (','); // delete a comma. The variable following the deletion is an array.
Char [] mychar = {'5', ','}; // Delete '5' and ','
S. trimend (mychar );
(12) three methods of split
A) use a single character to separate <string>. Split (New char [] {'character '}) // <string>. Split ('char ');
B) separated by multiple characters <string>. Split (New char [2] {'characters ',''})
C) use a string to separate RegEx. Split (<string>, "string", regexoptions. ignorecase );
(13) formats of several output strings
12345. tostring ("N"); // generate 12,345.00
12345. tostring ("C"); // generate $12,345.00
12345. tostring ("E"); // generate 1.234500e + 004
12345. tostring ("F4"); // generate 12345.0000
12345. tostring ("x"); // generate 3039 (hexadecimal)
12345. tostring ("P"); // generate 1,234,500.00%
(14) three methods to convert 123456789 to 12-345-6789
(A) A = int. parse (a). tostring ("########");
(B) A = A. insert (5, "-"). insert (2, "-");
(C) Using system. Text. regularexpressions; // first reference
RegEx Reg = new RegEx (@ "^ (d {2}) (d {3}) (d {4}) $ ");
A = reg. Replace (A, "$1-$2-$3 ");
(15) A simple striing STR = new string ('A', 21) that outputs 21 );
(16) method for obtaining a random number
Ramdom r = new ramdom ();
Int n1 = R. Next (); // return a non-negative random integer.
Int n2 = R. Next (10); // return a non-negative random integer smaller than the specified maximum value (10 ).
Int N3 = R. Next () % 10; // return a non-negative random integer smaller than the specified maximum value (10 ).
Int N4 = R. Next (); // return a specified range (1 ~ 20) random integer
Int N5 = R. nextdouble (); // obtain a value between 0.0 and ~ A random integer between 1.0
(17) int32.tryparse (), int32. parse (), convert. toint32 () comparison:
Both convert strings to integers.
Int32.tryparse (string, out INT );
Int = int32. parse (string );
Int = convert. toint32 (string );
Comparison: convert. if toint32 () is null, zero is returned instead of an exception. If int32. parse () is null, an exception is thrown. If int32.tryparse () is not thrown, true or false is returned to indicate whether the resolution is successful. If a parsing error occurs, the value of 0 will be obtained for the out call;
In terms of performance, int32.tryparse () is superior to int32.parse (), while int32.parse () is superior to convert. toint32 ().
Suggestion: Use int32.parse () in. net1.1 and int32.tryparse () in. net2.0 ().