7. C # basic sorting (class ),
String type
Concept: it is a class that contains many methods and attributes for processing strings.
1. length method. Example:
String s = "hello"; Console. WriteLine ("s Length: {0}", s. Length); // get the string Length and return the int Value
2. Trim & ToUpper method. Example:
String I = "hello"; Console. writeLine ("I value: {0}", I + "a"); Console. writeLine ("the value after I removes spaces is: {0}", I. trim () + "a"); Console. writeLine ("the value after I remove spaces on the left is: {0}", I. trimStart () + "a"); Console. writeLine ("the value after I remove spaces on the right is: {0}", I. trimEnd () + "a"); Console. writeLine ("I capital format: {0}", I. toUpper ());
3. indexof: Index starting from 0. Example:
String ss = "abcdefc"; Console. writeLine (ss. indexOf ("c"); // ctrl + shift + space first matching index Console. writeLine (ss. lastIndexOf ("c"); // The first index of the last matching item
4. startswith endswith: determines whether a string starts or ends with a Boolean value. Example:
bool b1 = ss.StartsWith("ab");bool b2 = ss.EndsWith("fc");Console.WriteLine(b1+","+b2);
5. contains: determines whether a string contains a specific character segment and returns a Boolean value. Example:
Console.WriteLine(ss.Contains("bc"));
6. substring: truncates a string. Example:
Console. writeLine ("the three characters starting from 2nd indexes are {0}", ss. substring (2, 3); // print the "3" character Console starting from the specified index "2. writeLine ("from the second index to the last string is {0}", ss. substring (2); // truncate from the specified index "2" to the end
7. tostring: convert to a string. Example:
DateTime date = DateTime. now; string sss = date. toString ("MM dd, yyyy, hh, mm, ss seconds"); Console. writeLine (sss); double dd = 1.234; string sss1 = dd. toString ("#. 00 "); // number of digits after the decimal point # number of digits before the decimal point # All digits. If the decimal point is 0, use". 00 ",". # "the Console will not be zero. writeLine (sss1 );
Math class
It is a variety of exponential calculation methods. You can try to enter Math to view its method. Here I only write one:
Math. Floor/Celing: Floor (minimum) and ceiling (maximum ). Example:
Console. writeLine ("3.14 floor value: {0}", Math. floor (3.14); // 3.00Console.WriteLine ("3.14 ceiling value: {0}", Math. ceiling (3.14); // 4.00.
Datetime type
1. now: Get the current system time
DateTime dt = DateTime. Now; dt = dt. AddYears (3); // Add three years to the current time (you can also write AddMonths, AddDays, AddHours, and so on)
Console. WriteLine (dt); // The output result is three more years later than the current time.
2. Use with TimeSpan:
DateTime da = new DateTime(1990, 01, 01);TimeSpan t = new TimeSpan(2,10,20,0) ;//TimeSpan(days,hours,minutes,seconds)da = da.Add(t);Console.WriteLine(da);
Console. Clear (); // Clear all information on the Console
Exercise
1. Enter an ID card number to cut your birthday
Console. writeLine ("Enter the id card number:"); string id = Console. readLine (); if (id. length = 18) {Console. writeLine ("birthday: {0} {1} {2}", id. substring (6, 4), id. substring (10, 2), id. substring (12, 2);} else Console. writeLine ("your input is incorrect ");View answer
2. Four Verification codes are randomly generated (0 ~ 9, ~ Z)
Random r = new Random (); string yan = "0123456789 abcdefghjklmnopqistuvwxyzABCDEFGHIJKLMNOPQISTUVWXYZ"; string yzm = ""; for (int j = 0; j <4; j ++) {int ra = r. next (yan. length); yzm = yan. substring (ra, 1) + yzm;} Console. writeLine (yzm );View answer
3. Simple mobile phone number lottery method through screen swiping
Write using Threading at the top
for (int j = 0; j < 20; j++) { Thread.Sleep(100); Console.Clear(); string shouji = "133333333333333"; Console.WriteLine(shouji); Thread.Sleep(100); Console.Clear(); shouji = "13344444444444444"; Console.WriteLine(shouji); }