Formatting String
s in Java can be a confusing and difficult task for beginners and experts alike. While the concept of String
formatting is simple, there are nearly endless combinations that can be easy to forget and frustrating to look up. In this article, we will outline the basic techniques for formatting String
s in Java using the venerable C-style and walk through easy-to-digest tables for all of the conversions and format specifiers available to us (that can be used as reference tables when we inevitably forget which format specifiers to use).
While not the focus of this article, we will also touch on the MessageFormat
class and how it can provide a simplified approach when the C-style approach is too cumbersome. Additionally, we will look at the various mechanisms we can use to format C-style conversions, including flags, widths, and precisions. Lastly, we will cover some important references that will help us to gain a deeper understanding of formatting String
s in Java.