C string common operations summary details

  • 2020-05-12 03:12:05
  • OfStack

C# string common operations summary details
(1) take the length of a string < string > .Length;
(2) string converted to bitcode GetBytes( < string > )
(3) string addition recommended StringBuilder sb = new StringBuilder(); sb. Append ( < string > );
(4) truncate 1 part of the variables of the string.
(5) check whether the specified position is the null character char.IsWhiteSpace (string variable, bit);
(6) check whether the character is the punctuation char.IsPunctuation(' character ');
(7) convert characters to Numbers, and look up the code point (int)' variable '
(8) turn the Numbers into characters and look up 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 ways to delete 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 (', '); // remove the comma, and the variable is any valid string
c) s. TrimEnd (', '); // remove the comma, and the variable that follows is the array
char [] mychar = {' 5 ', ', '}; // delete '5' and ', '
s.TrimEnd(mychar);
(12) three methods of Split
a) is separated by a single character < string > .Split (new char[]{' character '}) // < string > Split (' characters');
b) is separated by multiple characters < string > .Split(new char[2]{' character ',''})
c) to separate Regex.Split ( < string > , "string", RegexOptions.IgnoreCase);

(13) several output string formats
ToString (" n "); // generates 12,345.00
ToString (" C "); // generate ¥12,345.00
ToString (" e "); / / generated 1.234500 e + 004
ToString (" f4 "); / / generated 12345.0000
ToString (" x "); // generates 3039(hexadecimal)
ToString (" p "); / / generated 1234500.00%

(14) three methods for converting 123456789 into 12-345-6789
(a) A = int. Parse (a). ToString (" # # # # # - # # # # ");
(b) A = a. Insert (5, "-"). Insert (2, "-");
(c) Using System. Text. RegularExpressions; / / reference first
Regex reg = new Regex(@ "^(d{2})(d{3})(d{4})$");
A = reg. Replace (a, "$1 - $2 - $3");

(15) a simple method to output 21 A, striing str = new string(' A',21);
(16) the method of obtaining random Numbers
Ramdom r = new Ramdom();
Int n1 = r. Next (); // returns a nonnegative random integer
Int n2 = r. Next (10); // returns a non-negative random integer less than the specified maximum value (10)
Int n3 = r. Next () % 10; // returns a non-negative random integer less than the specified maximum value (10)
Int n4 = r. Next (1, 20); // returns a random integer in the specified range (1 to 20)
Int n5 = r. NextDouble (); // gets a random integer between 0.0 and 1.0

(17) Int32.TryParse (), Int32.Parse (), Convert.ToInt32 ()
Both convert a string to an integer number
Int32.TryParse(string,out int);
Int = Int32. Parse(string);
Int = Convert.ToInt32(string);
Convert.ToInt32 () returns zero instead of throwing an exception at null; Int32.Parse () throws an exception; Int32.TryParse () does not throw an exception, but returns either true or false to indicate whether the parsing was successful. If it is resolved incorrectly, the out call will get zero.
In terms of performance, Int32.TryParse () is superior to Int32.Parse (), while Int32.Parse () is superior to Convert.ToInt32 ().
Suggestion: Int32.Parse (); Int32.TryParse () is used under. NET2.0.

Related articles: