C # class,

Source: Internet
Author: User
Tags email account

C # class,

String class:
Length

. Trim () removes spaces at the beginning and end
. TrimStart () removes spaces starting with a string
. TrimEnd () removes spaces after the string

. ToUpper () all uppercase
. ToLower () all lower case

Substring (starting position, truncation length)
Substring (start position) only writes the start position, which can be truncated to the end
Birth date intercepted by ID card

IndexOf ("string") returns the index that appears for the first time
LastIndexOf ("string") returns the index of the last occurrence of this string

Whether StartsWith ("string") starts with this string, returns True or False
Whether EndsWith ("string") ends with this string
Whether Contains ("string") Contains this string. Returns True or False.

Replace ("Old Word", "New Word") replaces old words with new words

Example:
1. Enter your ID card number to capture your birthday
370321199003053330
Console. Write ("Enter your ID card number :");
String cid = Console. ReadLine ();
String year = cid. Substring (6, 4 );
String month = cid. Substring (10, 2 );
String day = cid. Substring (12, 2 );
Console. WriteLine ("Your birthdate is: {0}-{1}-{2. ", Year, month, day );
Console. ReadLine ();

 

2. Check whether the email format is correct.
1. There can be only one @
2. It cannot start @
3. @ followed by at least one.
4. @ and. cannot be together
5. cannot end.
Console. Write ("Enter your email account :");
String mail = Console. ReadLine ();
If (mail. Contains ("@"))
{
Int a = mail. IndexOf ("@");
Int B = mail. LastIndexOf ("@");
If (a = B)
{
If (! Mail. StartsWith ("@"))
{
String mail1 = mail. Substring ();
If (mail1.Contains ("."))
{
Int c = mail1.IndexOf (".");
If (c! = 1)
{
If (mail. Substring (a-1, 1 )! = ".")
{
If (! Mail. EndsWith ("."))
{
Console. WriteLine ("the email address format is correct. The email account you entered is:" + mail );
}
Else
{
Console. WriteLine ("your email format is incorrect! ");
}
}
Else
{
Console. WriteLine ("your email format is incorrect! ");
}
}
Else
{
Console. WriteLine ("your email format is incorrect! ");
}
}
Else
{
Console. WriteLine ("your email format is incorrect! ");
}
}
Else
{
Console. WriteLine ("your email format is incorrect! ");
}
}
Else
{
Console. WriteLine ("your email format is incorrect! ");
}
}
Else
{
Console. WriteLine ("your email format is incorrect! ");
}


Console. ReadLine ();

Math mathematics

 

Ceiling () Fetch online
Floor () Fetch offline
Math. PI circumference Rate
Math. Sqrt () Square Root
Math. Round () Rounding (note that the result of. 5 is different under an even number) is an odd number. 5 is an online number. 5 is an offline number.

 

DateTime time and date type

 

Note that you need to initialize it before using it.
DateTime dt = new DateTime ();
If you get the current time, you do not need to initialize it:
DateTime dt = DateTime. Now; // The current system time, which can be queried at runtime.
Obtain the Year dt. Year
Get Month dt. Month
Get dt. Day
Get Hour dt. Hour
Obtain dt. Minute
Get the Second dt. Second

Obtain the day of the week
DayOfWeek d = dt. DayOfWeek;
The obtained information is in English.
If you want to use Chinese characters, d. ToString () first ()
Then print out Chinese Characters in English.

Yyyy
MM Month
Dd Day
Hh hour
Mm
Ss seconds
The above is a bitwise character. This space can be occupied first in the string.
String s = dt. ToString ("MM minute ss seconds on mm dd, yyyy ");

DateTime can be added or subtracted from the corresponding time
Add () Increase or subtract a certain time interval
AddYears () Increase or subtract year
AddMonths () Increase or subtract the month
AddDays () Increase or subtract the number of days
And so on.
Note: plus or minus days, the number of hours is double type. All others are of the int type.

Example:

 


Obtain the day of the week
DayOfWeek d = dt. DayOfWeek;
// Console. WriteLine (d );
String dow = d. ToString ();
Switch (dow)
{
Case "Monday ":
Console. WriteLine ("Monday ");
Break;
Case "Tuesday ":
Console. WriteLine ("Tuesday ");
Break;
}

 

Add () Increase or decrease
TimeSpan span = new TimeSpan (3, 3, 3, 3 );
Console. WriteLine (dt. Add (span ));

 

// How many days are added
Console. WriteLine (dt. AddDays (-7.5 ));

 


Enter two time dates to calculate the number of different days (TotalDays)
Console. Write ("Enter the first time and date (****/**/****:**:**):");
DateTime dt1 = DateTime. Parse (Console. ReadLine ());
Console. Write ("enter the second time and date (****/**/****:**:**):");
DateTime dt2 = DateTime. Parse (Console. ReadLine ());

 

Console. WriteLine (dt2-dt1). TotalDays );

 

Countdown to College Entrance Examination
QQ lovers
Console. Write ("Enter the start date of your love affair :");
DateTime dt1 = DateTime. Parse (Console. ReadLine ());
DateTime dt2 = DateTime. Now;
Console. WriteLine ("already in love for {0} Days. ", Math. Ceiling (dt2-dt1). TotalDays ));

 

Console. ReadLine ();


Exception protection tryCatch
Try to protect statements that may cause errors
{
Console. Write ("enter an integer :");
Int a = int. Parse (Console. ReadLine ());
Console. WriteLine ("no error !! ");
}
If the statement in try has a problem, it will be caught directly and executed at this position.
{
Console. WriteLine ("your input is incorrect! ");
}
Finally what to do. Write is the same as do not write
{
Console. WriteLine ("Thank you for using it. Goodbye! ");
}
Console. ReadLine ();

 

Input date judgment format in if else, Simple Method
Try
{
Console. Write ("enter a time Date :");
DateTime dt = DateTime. Parse (Console. ReadLine ());
Console. WriteLine ("the time and date format you entered is correct! ");
}
Catch
{
Console. WriteLine ("the time and date you entered are incorrect! ");
}
Console. WriteLine ("Thank you! ");
Console. ReadLine ();

Random Number Random

 

Initialization
Random ran = new Random ();
Random number between 1 and one hundred: int a = ran. Next (101 );
Random Number of int B = ran. Next () in one );

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.