Memo/Java

Python f-string과 유사한 "MessageFormat"

l22hs 2023. 3. 16. 15:12

파이썬에서는 f-string을 정말 편리하게 사용해 왔었다.

하지만 아쉽게도 자바에서는 파이썬의 f-string과 동일한 기능을 하는 메소드는 없었다..

그래도 그나마 파이썬의 format 함수와 유사한 "MessageFormat"이란 것을 알게되었다.

String step1 = "one";
String step2 = "two";

String string = MessageFormat.format("Step {0} of {1}", step1, step2);

 

기존 String.Format( ) 메소드와 다르게 자료형을 별도로 지정해주지 않아도 된다.

 

 

ps.

검색 결과 Map을 이용해 f-string을 비슷하게나마 사용할 수는 있었다.

그러나 굳이굳이 이렇게 까지해서 사용하지는 않을 것 같다..😹

 

From the learnjava community on Reddit

Explore this post and more from the learnjava community

www.reddit.com