[Java] How to use string.format

What is string.format?

It is a method that formats and returns a character string based on the format specified in the argument.

How to write

The first argument must be written according to the specified format. After the second argument, pass the variable to be assigned to the format decided by the first argument.

public static String format(String format,Object... args)

Implementation example

Returns a formatted string with the specified formatted string and arguments. ** Specify the format by writing (% ~) **. ** Use d to format integers (decimal integers) and s ** to format strings.

        int year = 2021;
        String str = String.format("This year%It's d years.", year);
        System.out.println(str); //This year is 2021.

        //Add 0 to the beginning
		System.out.println(String.format("%05d", 1000)); //01000
		System.out.println(String.format("%06d", 1000)); //001000

		//Add characters
		System.out.println(String.format("%d and%d", 1000,2000)); //1000 and 2000

		//Format string
		System.out.println(String.format("%s/%s/%s", "2021","01","03")); //2021/01/03

Recommended Posts

[Java] How to use string.format
[Java] How to use Map
[Java] How to use Map
How to use java class
[Java] How to use Optional ②
[Java] How to use removeAll ()
How to use Java Map
How to use Java variables
[Java] How to use Optional ①
How to use Java HttpClient (Get)
How to use Java HttpClient (Post)
[Java] How to use join method
[Processing × Java] How to use variables
[Java] How to use LinkedHashMap class
[JavaFX] [Java8] How to use GridPane
How to use class methods [Java]
[Java] How to use List [ArrayList]
How to use classes in Java?
[Processing × Java] How to use arrays
How to use Java lambda expressions
[Java] How to use Math class
How to use Java enum type
Multilingual Locale in Java How to use Locale
How to use Map
How to use rbenv
How to use letter_opener_web
How to use fields_for
How to use java.util.logging
How to use map
[Java] How to use the HashMap class
How to use collection_select
[Easy-to-understand explanation! ] How to use Java instance
[Java] How to use the toString () method
Studying how to use the constructor (java)
[Processing × Java] How to use the loop
How to use Twitter4J
How to use active_hash! !!
How to use MapStruct
How to use hidden_field_tag
How to use TreeSet
[How to use label]
How to use identity
How to use hashes
How to use Java classes, definitions, import
[Easy-to-understand explanation! ] How to use Java polymorphism
[Java] [Maven3] Summary of how to use Maven3
[Processing × Java] How to use the class
How to use Java Scanner class (Note)
How to use JUnit 5
[Processing × Java] How to use the function
[Easy-to-understand explanation! ] How to use ArrayList [Java]
How to use Dozer.mapper
[Java] How to use the Calendar class
How to use Gradle
How to use org.immutables
How to use java.util.stream.Collector
How to use VisualVM
[Java] Learn how to use Optional correctly
[Easy-to-understand explanation! ] How to use Java overload
How to use Map
try-catch-finally exception handling How to use java