前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Java String Format 使用实例

Java String Format 使用实例

作者头像
一个会写诗的程序员
发布2021-12-16 11:33:09
4620
发布2021-12-16 11:33:09
举报
文章被收录于专栏:一个会写诗的程序员的博客

Always forgetting the Java String formatting specifiers? Or maybe you never took the time to learn. Here's a reference of the various flags you can use.

Have you tried to read and understand Java’s String format documentation? I have and found it nearly impenetrable. While it does include all the information, the organization leaves something to be desired.

This guide is an attempt to bring some clarity and ease the usage of string formatting in Java. You may also want to take a look at What's New in Java 8.

String Formatting

The most common way of formatting a string in java is using String.format(). If there were a “java sprintf” then this would be it.

代码语言:javascript
复制
String output = String.format("%s = %d", "joe", 35);

For formatted console output, you can use printf() or the format() method of System.out and System.err PrintStreams.

代码语言:javascript
复制
System.out.printf("My name is: %s%n", "joe");

Create a Formatter and link it to a StringBuilder. Output formatted using the format() method will be appended to the StringBuilder.

代码语言:javascript
复制
 StringBuilder sbuf = new StringBuilder();
 Formatter fmt = new Formatter(sbuf);
 fmt.format("PI = %f%n", Math.PI);
 System.out.print(sbuf.toString());

// you can continue to append data to sbuf here.

Format Specifiers

Here is a quick reference to all the conversion specifiers supported:

https://dzone.com/articles/java-string-format-examples

Argument Index: %1$s

An argument index is specified as a number ending with a “$” after the “%” and selects the specified argument in the argument list.

代码语言:javascript
复制
String.format("%2$s", 32, "Hello"); // prints: "Hello"

Formatting an Integer

With the %d format specifier, you can use an argument of all integral types including byte, short, int, long and BigInteger.

Default formatting:

代码语言:javascript
复制
String.format("%d", 93); // prints 93
Specifying a width:
String.format("|%20d|", 93); // prints: |                  93|
Left-justifying within the specified width:
String.format("|%-20d|", 93); // prints: |93                  |
Pad with zeros:
String.format("|%020d|", 93); // prints: |00000000000000000093|
Print positive numbers with a “+”:
 (Negative numbers always have the “-” included):
String.format("|%+20d|', 93); // prints: |                 +93|
A space before positive numbers.
A “-” is included for negative numbers as per normal.
String.format("|% d|", 93); // prints: | 93| String.format("|% d|", -36); // prints: |-36|
Use locale-specific thousands separator:
For the US locale, it is “,”:
String.format("|%,d|", 10000000); // prints: |10,000,000|
Enclose negative numbers within parentheses (“()”) and skip the "-":
String.format("|%(d|", -36); // prints: |(36)|
Octal output:
String.format("|%o|"), 93); // prints: 135
Hex output:
String.format("|%x|", 93); // prints: 5d
Alternate representation for octal and hex output:
Prints octal numbers with a leading “0” and hex numbers with leading “0x“.
String.format("|%#o|", 93);
// prints: 0135
String.format("|%#x|", 93);
// prints: 0x5d
String.format("|%#X|", 93);
// prints: 0X5D
String and Character Conversion
Default formatting:
Prints the whole string.
String.format("|%s|", "Hello World"); // prints: "Hello World"
Specify Field Length
String.format("|%30s|", "Hello World"); // prints: | Hello World|
Left Justify Text
String.format("|%-30s|", "Hello World"); // prints: |Hello World |
Specify Maximum Number of Characters
String.format("|%.5s|", "Hello World"); // prints: |Hello|
Field Width and Maximum Number of Characters
String.format("|%30.5s|", "Hello World"); | Hello|

Summary

This guide explained String formatting in Java. We covered the supported format specifiers. Both numeric and string formatting support a variety of flags for alternative formats. If you want more content on Java Strings, check out the Do's and Don'ts of Java Strings.

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021/8/11 下,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • String Formatting
  • Format Specifiers
  • Argument Index: %1$s
  • Formatting an Integer
  • Summary
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档