C # placeholders and formatted strings

Source: Internet
Author: User
Tags iso 8601 string format try catch

static void Main ()           {                   string c=console.readline ();                   String D=console.readline ();                  Console.WriteLine (c+ "," +d);    Use "+" connector         }

  

You say it is easy to write wrong, very troublesome, C # also provides another way of writing, is a placeholder, with {} to indicate, in {} Fill in the number of bits occupied, C # provisions starting from 0, that is, the output just now, we can also represent Response.Write ("{0},{1}", C,D); There are two bit c,d here, so you need two placeholders so we write {0},{1}, and we need to note that placeholders are written in "".
static void Main ()           {                   string c=console.readline ();                   String D=console.readline ();                   String M=string.format ("{0},{1}", c,d);   String format output                   Response.Write (m);            }

  

You can see that the output is exactly the same. Here the string is a class, format is one of the methods used to format the output character. We know that in the real life sometimes need special expression characters, such as currency, time, then what to do? Do not worry, C # in the format of identifiers, the following shows you a few common formatting identifiers letter meaning
C or C Currency currency format
D or D decimal decimal format (decimal integer, not and. NET is confused with the decimal data type)
E or e Exponent exponential format
F or f fixed point precision format
G or G General format
n or n divides the number of thousands with commas, for example 1234 will be changed to 1,234
P or p Percentage percent symbol format
R or R round-trip rounding (floating point only) ensures that a number is converted into a string and can be reversed to the same number.
x or x 16 binary format
protected void Page_Load (object sender, EventArgs e)             {                   int i=12345;                   Response.Write ("{0:c}", i);   Currency                   Response.Write ("{0:d}", i);   Decimal number                   Response.Write ("{0:e}", i);    Science and Technology Law                   Response.Write ("{0:f}", i);   Floating-point notation                   Response.Write ("{0:g}", i);   G or G General common format                   Response.Write ("{0:n}", i);   n or n divides the number of thousands with commas         }

  

1. Processing and formatting of numerical values when checking what data type the user enters, only the TryParse () method of the value type is called to examine the data contents.   The difference between the TryParse () and the parse () method is that the former returns a bool value, which, if the conversion is unsuccessful, throws an exception and needs to be caught with a try catch statement. The following is a check whether user input is a number
    protected void Page_Load (object sender, EventArgs e)    {        string s1 = "300";        string s2 = "n";        String s3 = "2,000";        String S4 = " -1000";        String S5 = "4000d";        int currentvalue = 0;        Response.Write (Int. TryParse (S1, out CurrentValue));     Returns false        Response.Write (int. TryParse (S2, out currentvalue));     Returns True        Response.Write (int. TryParse (S3, out CurrentValue));     Returns false        Response.Write (int. TryParse (S4, out CurrentValue));     Returns True        Response.Write (int. TryParse (S5, out CurrentValue));     returns false         }

  

Numeric formatting: If you have decimal and float type number processing, use the post character m or F, such as: decimal d = 12345.67M;    float f =3.1415f; Otherwise, the number will be treated as a double type, resulting in a compilation error, if the integer is not plus M. 2. Processing and formatting of letters
protected void Page_Load (object sender, EventArgs e) {string str = "Do y        ou like ASP. "; Response.Write (str.      ToUpper ()); Convert to uppercase Response.Write (str.      ToLower ()); Convert to lowercase} 

D mm/dd/yyyy ShortDatePattern (short date mode)  
D dddd,mmmm dd,yyyy longdatepattern (long date mode)  
F dddd,mmmm DD, YYYY hh:mm Full date and time (long date and short times)  
F dddd,mmmm dd,yyyy HH:mm:ss Fulldatetimepatte RN (long date and long time)  
G mm/dd/yyyy hh:mm General (short date and short time) (common mode, shorter dates and shorter times)  < Br>g mm/dd/yyyy HH:MM:SS General (short date and long time)  
M,m MMMM DD Monthdaypattern (Month-day mode)   ;
R,r ddd,dd MMM yyyy,hh ': ' mm ': ' ss ' GMT ' Rfc1123pattern (RFC1123 mode)  
S yyyy-mm-dd HH:mm:ss Sortabledatetimepattern (conforms to ISO 8601) using local time (using the sortable mode of native times)  
T hh:mm Shorttimepattern (short time mode) & nbsp
T HH:mm:ss Longtimepattern (long time mode)  
U yyyy-mm-dd HH:mm:ss Universalsortable-datetimepattern (conforms to ISO 8601) using Universal Time (Universal sortable mode)  
U dddd,mmmm Dd,yyyy,hh:mm:ss Universalsortable-datetimepattern (Universal sortable mode)  
Y,y mmmm,yyyy Yearmonthpattern (year-month mode)

static void Main ()            {                  Response.Write ("{0:d}", DateTime.Now);   Output to day                   Response.Write ("{0:y}", DateTime.Now);   Output to month                   Response.Write ("{0:m}", DateTime.Now);    Take out is that month                   Response.Write ("{0:t}", DateTime.Now);   Take a long time to the second                   Response.Write ("{0:t}", DateTime.Now);   Take short time to sub                   Response.Write ("{0:tt}", DateTime.Now);   Take out the morning or the afternoon              }

  

C # placeholders and formatted strings

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.