Formatting strings in java

前端 未结 3 2076
甜味超标
甜味超标 2020-12-15 12:20

I am reading the serial buffer with readLine() method. The string returned by readLine() is in the format \"str1 : str2\". In a while loop when I use readLine() number of ti

相关标签:
3条回答
  • 2020-12-15 12:56

    The easiest way to achieve this is to use System.out.format(). This lets you use C printf style format specifiers. An example:

    System.out.format("%-5s:%10s\n", "Name", "Nemo");
    System.out.format("%-5s:%10d\n", "Age", 120);
    

    This will output:

    Name :      Nemo
    Age  :       120
    

    Also see documentation for the Formatter class to learn more about the format string syntax.

    0 讨论(0)
  • 2020-12-15 13:10

    Take a look at System.out.printf

    0 讨论(0)
  • 2020-12-15 13:14

    System.out.format("%30s : %s", str1, str2);

    Will print the first string with a fixed width of 30 characters.

    0 讨论(0)
提交回复
热议问题