The format() function

Perhaps, the most interesting of the String class's methods is actually one of its static methods: String.format(). To show you the power of String.format(), I'd like to create a brand new functional class for our project. So right-click on the Project name in the filesystem, shown on the left-hand side of the screen, create a new class, and call it CustomPrinter.java:

package strings; 
 
public class Strings { 
 
    public static void main(String[] args) { 
        CustomPrinter printer = new CustomPrinter("> > %s < <"); 
         
        String s1 = new String        ("Strings are arrays of characters"); 
        String s2 = new String        ("Strings are arrays of characters"); 
         
        printer.println ("string1: " + s1.replace("characters", "char")); printer.println("string2: " ...

Get Java Programming for Beginners now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.