当前位置:网站首页>C # basic knowledge (3)
C # basic knowledge (3)
2022-07-03 23:06:00 【BeanInJ】
List of articles
Write it at the front
Main contents of this paper :Console class 、Math class 、Random class 、DateTime class 、Regex class 、 String common operations 、 Data conversion
1、C# Several common classes provided
1.1、Console class
Common methods
Method | describe |
---|---|
Write | Output content to the console without wrapping |
WriteLine | Output content to the console and wrap |
Read | Read a character from the console |
ReadLine | Read a line of characters from the console |
1.2、Math class
Method | describe |
---|---|
Abs | Take the absolute value |
Ceiling | Returns the smallest integer value greater than or equal to the specified double precision floating-point number |
Floor | Returns the maximum integer value less than or equal to the specified double precision floating-point number |
Equals | Returns whether the specified object instances are equal |
Max | Returns the value of the larger of two numbers |
Min | Returns the decimal value of two numbers |
Sqrt | Returns the square root of a specified number |
Round | Returns the rounded value |
1.3、Random class
Method | describe |
---|---|
Next() | Each time a different random positive integer is generated |
Next(int max Value) | To produce a ratio max Value Small positive integers |
Next(int min Value,int max Value) | Produce a minValue~maxValue The positive integer , But does not contain maxValue |
NextDouble() | Produce a 0.0~1.0 Floating point number |
NextBytes(byte[] buffer) | To fill an array of specified bytes with random numbers |
1.4、DateTime class
Method | describe |
---|---|
Date | Get the date part of the instance |
Day | Get the date represented by the instance is the day of a month |
DayOfWeek | Get the day of the week represented by the instance |
DayOfYear | The date represented by this instance is the day of the year |
Add(Timespan value) | Add a time interval value to the specified date instance value |
AddDays(double value) | Add the specified number of days to the specified date instance value |
AddHours(double value) | Adds the specified number of hours to the specified date instance value |
AddMinutes(double value) | Adds the specified number of minutes to the specified date instance value |
AddSeconds(double value) | Adds the specified number of seconds to the specified date instance value |
AddMonths(int value) | Add the specified month to the specified date instance value |
AddYears (int value) | Adds the specified year to the specified date instance value |
example
class Program
{
static void Main(string[] args)
{
DateTime dt = DateTime.Now;
Console.WriteLine(" Current date is :{0}", dt);
Console.WriteLine(" The current time is the... Of this month {0} God ", dt.Day);
Console.WriteLine(" Now it's :{0}", dt.DayOfWeek);
Console.WriteLine(" The current year is {0} God ", dt.DayOfYear);
Console.WriteLine("30 The date in days is {0}", dt.AddDays(30));
DateTime dt1 = DateTime.Now;
DateTime dt2 = new DateTime(2019, 6, 1);
TimeSpan ts = dt2 - dt1;
Console.WriteLine(" The number of days between is {0} God ", ts.Days);
}
}
1.5、Regex class ( Regular expressions )
Metacharacters in regular expressions
character | describe |
---|---|
. | Match all characters except newline |
\w | Match the letter 、 Numbers 、 Draw the line down |
\s | Match blanks ( Space ) |
\d | Match the Numbers |
\b | Match the beginning or end of the expression |
^ | Match the beginning of the expression |
$ | Match the end of the expression |
A character that represents a repetition in a regular expression
word operator | Sketch Statement |
---|---|
* | 0 One or more characters |
? | 0 Time or 1 Sub characters |
+ | 1 One or more characters |
{n} | n Sub characters |
{n,M} | n To M Sub characters |
{n, } | n More than characters |
class Program
{
static void Main(string[] args)
{
Console.WriteLine(" Please enter a mailbox ");
string email = Console.ReadLine();
Regex regex = new Regex(@"^(\w)+(\.\w)*@(\w)+((\.\w+)+)$");
if (regex.IsMatch(email))
{
Console.WriteLine(" The email format is correct .");
}
else
{
Console.WriteLine(" The email format is incorrect .");
}
}
}
2、 String common operations
class Program
{
static void Main(string[] args)
{
// To obtain the length of the
string str = " This is a string !";
Console.WriteLine(" The length of the string is :" + str.Length);
Console.WriteLine(" The first character in the string is :" + str[0]);
Console.WriteLine(" The last character in the string is :" + str[str.Length - 1]);
// Character position , No return -1
int i = str.IndexOf("@");
// Character substitution
str = str.Replace("!", ".");
// Character interception
str = str.Substring(0, 2);
// Character insertion
str = str.Insert(1, ",");
}
}
3、 Data conversion
3.1、 Implicit conversion
int a = 100;
double d = a; // take int Type conversion to double type
float f = 3.14f;
d = f; // take float Type conversion to double type
3.2、 Explicit conversion
Method | describe |
---|---|
ToBoolean | If possible , Convert type to Boolean . |
ToByte | Convert type to byte type . |
ToChar | If possible , Convert the type to a single Unicode Character type . |
ToDateTime | Put the type ( Integer or string type ) Convert to date - Time structure . |
ToDecimal | Converts a floating point or integer type to a decimal type . |
ToDouble | Convert type to double precision floating point type . |
ToInt16 | Convert type to 16 Bit integer type . |
ToInt32 | Convert type to 32 Bit integer type . |
ToInt64 | Convert type to 64 Bit integer type . |
ToSbyte | Convert type to signed byte type . |
ToSingle | Convert type to small floating-point type . |
ToString | Convert type to string type . |
ToType | Converts the type to the specified type . |
ToUInt16 | Convert type to 16 Bit unsigned integer type . |
ToUInt32 | Convert type to 32 Bit unsigned integer type . |
ToUInt64 | Convert type to 64 Bit unsigned integer type . |
3.3、 Coercive transformation
double dbl_num = 12345678910.456;
int k = (int) dbl_num ;// Cast is used here
3.4、 Parse Methods and ToString Method
Parse Method is used to convert a string type to any type
int num1 = int.Parse("12348");
ToString Method is used to convert any data type to a string type
int a=100;
string str=a.ToString();
3.5、Convert Method
It can convert the value of any data type into any data type , The premise is not to exceed the range of the specified data type .
Method | explain |
---|---|
Convert.ToInt16() | Convert to integer (short) |
Convert.ToInt32() | Convert to integer (int) |
Convert.ToInt64() | Convert to integer (long) |
Convert.ToChar() | Convert to character (char) |
Convert.ToString() | Convert to string (string) |
Convert.ToDateTime() | Convert to date type (datetime) |
Convert.ToDouble() | Convert to double precision floating point (double) |
Conert.ToSingle() | Convert to single precision floating point (float) |
class Program
{
static void Main(string[] args)
{
float num1 = 82.26f;
int integer;
string str;
integer = Convert.ToInt32(num1);
str = Convert.ToString(num1);
Console.WriteLine(" Value converted to integer data {0}", integer);
Console.WriteLine(" Convert to string {0},",str);
}
}
3.6、 Packing and unpacking ( Value type and reference type )
int val = 100;
object obj = val;
Console.WriteLine(" The value of the object = {0}", obj);
// This is a packing process , Is the process of converting a value type to a reference type
int val = 100;
object obj = val;
int num = (int) obj;
Console.WriteLine("num: {0}", num);
// It's an unpacking process , Is to convert a value type to a reference type , The process of converting a reference type to a value type
边栏推荐
- IDENTITY
- NPM script
- Exclusive download! Alibaba cloud native brings 10 + technical experts to bring "new possibilities of cloud native and cloud future"
- Arc135 partial solution
- [flax high frequency question] leetcode 426 Convert binary search tree to sorted double linked list
- Pyqt5 sensitive word detection tool production, operator's Gospel
- Schematic diagram of crystal oscillator clock and PCB Design Guide
- Harbor integrated LDAP authentication
- 320. Energy Necklace (ring, interval DP)
- . Net ADO splicing SQL statement with parameters
猜你喜欢
Summary of basic knowledge of exception handling
Yyds dry goods inventory Spring Festival "make" your own fireworks
How to quickly build high availability of service discovery
User login function: simple but difficult
Summary of fluent systemchrome
Pointer concept & character pointer & pointer array yyds dry inventory
Teach you to easily learn the type of data stored in the database (a must see for getting started with the database)
Hcip day 15 notes
Qtoolbutton available signal
Leetcode: a single element in an ordered array
随机推荐
How to connect a laptop to a projector
How can enterprises and developers take advantage of the explosion of cloud native landing?
Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?
NPM script
Go error collection | talk about the difference between the value type and pointer type of the method receiver
Wisdom tooth technology announced that it had completed the round D financing of US $100million and had not obtained a valid patent yet
Unique in China! Alibaba cloud container service enters the Forrester leader quadrant
Label coco format data and format data in the upper left corner and lower right corner are mutually converted
Programming language (1)
Scratch uses runner Py run or debug crawler
Quick one click batch adding video text watermark and modifying video size simple tutorial
Mongoose the table associated with the primary key, and automatically bring out the data of another table
Qtoolbutton - menu and popup mode
[sg function]split game (2020 Jiangxi university student programming competition)
1 Introduction to spark Foundation
Hcip day 14 notes
Hcip 13th day notes
C deep anatomy - the concept of keywords and variables # dry inventory #
How to understand the gain bandwidth product operational amplifier gain
Exclusive download! Alibaba cloud native brings 10 + technical experts to bring "new possibilities of cloud native and cloud future"