๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
BackEnd๐ŸŒฑ/Java

[Java] String format() method

by dkswnkk 2022. 5. 14.

String format() method

java string format() ๋ฉ”์„œ๋“œ๋Š” ์ง€์ •๋œ Locale, format ๋ฐ arguments๋กœ ํ˜•์‹์ด ์ง€์ •๋œ ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. String.format() ๋ฉ”์„œ๋“œ์—์„œ Locale(๊ตญ๊ฐ€)์„ ์ง€์ •ํ•˜์ง€ ์•Š์œผ๋ฉด Locale.getDefault() ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ๊ธฐ๋ณธ Locale์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

Java์—์„œ์˜ format() ๋ฉ”์„œ๋“œ๋Š” C ์–ธ์–ด์˜ printf() ํ•จ์ˆ˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

์„ธ๋ถ€ ๊ตฌํ˜„ 

String format() ๋ฉ”์„œ๋“œ์˜ ์„ธ๋ถ€ ๊ตฌํ˜„์€ ์•„๋ž˜์™€ ๊ฐ™์œผ๋ฉฐ, ๋‘ ๊ฐ€์ง€ ์œ ํ˜•์ด ์žˆ์Šต๋‹ˆ๋‹ค.

public static String format(String format, Object... args) {
        return new Formatter().format(format, args).toString();
    }

   
public static String format(Locale l, String format, Object... args) {
    	return new Formatter(l).format(format, args).toString();
    }

 

๋งค๊ฐœ๋ณ€์ˆ˜

  • Locale: format() ๋ฉ”์„œ๋“œ์— ์ ์šฉํ•  ๊ตญ๊ฐ€๋ฅผ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค.
  • format: ๋ฌธ์ž์—ด์˜ ํ˜•์‹ ์ฆ‰, ์ •์ˆ˜์ธ์ง€ ์†Œ์ˆ˜์ธ์ง€ ๋“ฑ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.
  • args: ํ˜•์‹ ๋ฌธ์ž์—ด์— ๋Œ€ํ•œ ์ธ์ˆ˜๋กœ์„œ, 0 ์ด์ƒ์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋‘ ๊ฐ€์ง€ ์œ ํ˜•์€ Locale์„ ์ง€์ •ํ•ด์ฃผ๊ณ  ์•ˆ ํ•ด์ฃผ๊ณ ์˜ ์ฐจ์ด์ธ๋ฐ, Locale์„ ์ง€์ •ํ•˜๋Š” ๊ฒฝ์šฐ๋Š” ์•„๋ž˜์™€ ๊ฐ™์€ ํ˜•์‹์œผ๋กœ ์ด์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

String str = String.format( Locale.KOREA, "test %f", 12.123f );

Locale์„ ์ง€์ •ํ•ด์ฃผ๋Š” ์œ ํ˜•์˜ ๊ฒฝ์šฐ ์•„์ง ๋ฐฑ์—”๋“œ ๊ฐœ๋ฐœ์—์„œ๋Š” ๊ฒช์–ด ๋ณด์ง€ ๋ชปํ–ˆ์ง€๋งŒ ์•ˆ๋“œ๋กœ์ด๋“œ ๊ฐœ๋ฐœ์—์„œ ์œ„๋„, ๊ฒฝ๋„๋ฅผ ๋ถˆ๋Ÿฌ์˜ฌ ๋•Œ Locale์„ ์ง€์ •ํ•ด ์ฃผ์ง€ ์•Š์•˜์„ ๋•Œ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

 

์˜ˆ์™ธ

  • NullPointerException: ํ˜•์‹์ด null์ผ ๊ฒฝ์šฐ.
  • IllegalFormatException: ํ˜•์‹์ด ์ž˜๋ชป๋˜์—ˆ๊ฑฐ๋‚˜ ํ˜ธํ™˜ํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ

 

format ์ข…๋ฅ˜

ํ˜•์‹ ์ง€์ •์ž ๋ฐ์ดํ„ฐ ํ˜•์‹ ์ถœ๋ ฅ
%a ๋ถ€๋™ ์†Œ์ˆ˜์ (BigDecimal ์ œ์™ธ) ๋ถ€๋™ ์†Œ์ˆ˜์  ์ˆซ์ž์˜ 16์ง„์ˆ˜ ์ถœ๋ ฅ
%b ๋ชจ๋“  ์œ ํ˜• null์ด ์•„๋‹๋•Œ true, null์ด๋ฉด false
%c character ์œ ๋‹ˆ์ฝ”๋“œ ๋ฌธ์ž
%d ์ •์ˆ˜(byte, short, int, long, bigint ํฌํ•จ) 10์ง„์ˆ˜ ์ •์ˆ˜
%e ๋ถ€๋™ ์†Œ์ˆ˜์  10์ง„์ˆ˜
%f ๋ถ€๋™ ์†Œ์ˆ˜์  10์ง„์ˆ˜
%g ๋ถ€๋™ ์†Œ์ˆ˜์  10์ง„์ˆ˜
%h   hashCode()๋ฉ”์„œ๋“œ์—์„œ ๊ฐ€์ ธ์˜จ ๊ฐ’์˜ 16์ง„์ˆ˜ ๋ฌธ์ž์—ด
%n   ์ค„ ๊ตฌ๋ถ„
%o ์ •์ˆ˜(byte, short, int, long, bigint ํฌํ•จ) 8์ง„์ˆ˜
%s   ๋ฌธ์ž์—ด
%t ๋‚ ์งœ/์‹œ๊ฐ„ ๋‚ ์งœ/์‹œ๊ฐ„
%x ์ •์ˆ˜(byte, short, int, long, bigint ํฌํ•จ) 16์ง„์ˆ˜

 

์˜ˆ์ œ1

public class Test {
    public static void main(String[] args) {
        String name = "sonoo";
        String sf1 = String.format("name is %s", name);
        String sf2 = String.format("value is %f", 32.33434);
        String sf3 = String.format("value is %32.12f", 32.33434);   //returns 12 char fractional part filling with 0

        System.out.println(sf1);
        System.out.println(sf2);
        System.out.println(sf3);
    }
}

 

์˜ˆ์ œ 2

public class Test {
    public static void main(String[] args) {
        String str1 = String.format("%d", 101);          // Integer value
        String str2 = String.format("%s", "Amar Singh"); // String value
        String str3 = String.format("%f", 101.00);       // Float value
        String str4 = String.format("%x", 101);          // Hexadecimal value
        String str5 = String.format("%c", 'c');          // Char value
        System.out.println(str1);
        System.out.println(str2);
        System.out.println(str3);
        System.out.println(str4);
        System.out.println(str5);
    }
}

 

์˜ˆ์ œ 3

ํ˜•์‹ ์ง€์ • ์™ธ์—๋„ ์•„๋ž˜์™€ ๊ฐ™์ด ๋ฌธ์ž์—ด์˜ ๋„ˆ๋น„, ํŒจ๋”ฉ ๋“ฑ์„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

    public static void main(String[] args) {
        String str1 = String.format("%d", 101);
        String str2 = String.format("|%10d|", 101);  // Specifying length of integer
        String str3 = String.format("|%-10d|", 101); // Left-justifying within the specified width
        String str4 = String.format("|% d|", 101);
        String str5 = String.format("|%010d|", 101); // Filling with zeroes
        System.out.println(str1);
        System.out.println(str2);
        System.out.println(str3);
        System.out.println(str4);
        System.out.println(str5);
    }

 

์ฐธ๊ณ 

 

String.format() ์‚ฌ์šฉ ์‹œ ์ฃผ์˜ํ•  ๊ฒƒ

์–ผ๋งˆ์ „ ์œ„๋„ ๊ฒฝ๋„๋ฅผ ๊ฐ€๊ณตํ•˜๊ธฐ ์œ„ํ•ด, ์•„๋ž˜์™€ ๊ฐ™์€ ์ฝ”๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค.

medium.com

 

Java String format() method - javatpoint

Java String format() method with method signature and examples of concat, compare, touppercase, tolowercase, trim, length, equals, split, string format in java etc.

www.javatpoint.com

๋Œ“๊ธ€