Java C # placeholders in C

Source: Internet
Author: User

Java C # placeholders in C
It is common to splice a string in programming. The following is a summary: What is a placeholder? A placeholder occupies a fixed position, waiting for you to add the content symbol to it. 1. java: copy the code package com. amos; import java. text. messageFormat;/*** Created by amosli on 14-7-24. */public class Test {public static void main (String args []) {// concatenate a string common method String name = "amosli"; // Method 1: string result = "hello," + name; System. out. println (result); // hello, amosli // Method 2: result = "hello ,". concat (name); System. out. println (result); // hello, amosli/ /Method 3: result = String. format ("% d % s", 200, "Yuan"); System. out. println (result); // $200 // Method 4: result = MessageFormat. format ("hi, {0}, I am {1}", "Jack", "Amosli"); System. out. println (result); // hi, Jack, I am Amosli} copy the code. Note: method 1 should be the most commonly used and use the + connector directly; method 2: Use the concat () method of the String class for connection, which is similar to the + number. Method 3. the placeholder is used, and the String is used in the third method. the format () method. Note that the content to be replaced must be % s in general, and % d in general. in method 4, {n} is directly used, where n represents the order of parameters, which is very similar to that in C. 2.C# Medium Static void Main () {string c = "hello, amosli"; string m = String. format ("{0}", c); // output Console in string Format. writeLine (m); // hello, amosli: letter meaning C or c Currency format D or d Decimal format (Decimal integer, do not match., for example, 1234 will be changed to 1, 234P, or p Percentage percentile format R or r Round-trip Round (for floating point only) make sure that a number is converted into a string and then converted back to the same number X or x Hex hexadecimal format. For example Under (see cs_6.cs) static void Main () {int I = 12345; Console. writeLine ("{0: C}", I); // currency Console. writeLine ("{0: D}", I); // Console in decimal number. writeLine ("{0: E}", I); // scientific and technological method Console. writeLine ("{0: F}", I); // floating point representation Console. writeLine ("{0: G}", I); // G or g General common format Console. writeLine ("{0: N}", I); // N or n use commas to separate thousands of digits} copy the code. Note: if you use the @ symbol. he will ignore all escape characters. that is, {1} is {1} rather than the placeholder. that is, "@ {1}" is not recognized as a placeholder. 3. the placeholder in C copies the format placeholders in the C language: % a, % Read a floating point value (valid only for C99) % c read a character % d read a decimal integer % I read a decimal, octal, hexadecimal integer % o read an octal integer % x, % X reads a hexadecimal integer % s into a string, ending with spaces, tabs, or line breaks. % F, % F, % e, % E, % g, and % G are used to input real numbers. They can be input in decimal or exponential form. % P read a pointer % u read an unsigned decimal integer % n equal to the number of characters % [] scanned Character Set % read % symbol format input and output example scanf ("% d, % d, % d ", & a, & B, & c); // enter three integers from the keyboard, and use commas to separate scanf (" % c ", & s ); // enter a character scanf ("% f", & f) from the keyboard; // enter a floating point data printf ("% d \ n", a) from the keyboard ); // output an integer printf ("% f \ n", B); // output a floating point number printf ("% s \ n", c ); // output a copy of a character code can basically see three very similar, Java and C # basically still copy C language.

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.