Format output using printf and String.Format

Source: Internet
Author: User
Formatted output

In which cases the formatted output is used:

    • The use of formatted output in the abnormal print to log is helpful to troubleshoot the cause of the error;
printf format

Example:

  public class PrintfTest {    public static void main(String[] args) {        printfTest1("小明",123456);    }    private static void printfTest1(String name, Integer fansCount) {        System.out.printf("你好,%s,粉丝%d人",name,fansCount);    }  }     

PrintfTest1 () Description:

    • %: Characters starting with% will be replaced with corresponding parameters;
    • S: Represents a string;
    • D: Represents a decimal integer.

Other translator descriptions:

Conversion character type
D Decimal integer
X hexadecimal integer
O Eight-binary integers
F Single-precision floating-point number
E Exponential floating-point number
G Universal floating point number
A Hexadecimal floating-point number
S String
C Character
B Boolean
H Hash code
String.Format formatting

Difference: String.Format does not print output, the other uses the same as printf.
Example:

    // 作为一个变量,可以传入日志info或error中,方便排查bug    String temp = String.format("%1$s ,测试, %2$s","小明","测试");

This is my first article, but also the first time to use Markdown, the composition of the article needs to be slowly pondering. has been practicing Java development for a year, due to the weak foundation, the development of the hole more and more, taking advantage of this time to re-understand the basic knowledge of Java.

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.