Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update String with parameters

I'm using Spring with Java 8.

I have a string with parameters and I am not sure how should I updates the parameters by list of params.

 String message = "Today is {0} and {1} is happy";
 List<String> params = Arrays.asList("Sunday", "Uri", "Dan");

I would like to know with which operator should I use in order to get :

  String newMessage = "Today is Sunday and Uri is happy.";

Thanks you

like image 742
userit1985 Avatar asked Apr 07 '26 03:04

userit1985


1 Answers

You can use MessageFormat like this :

String result = MessageFormat.format(message, params.toArray());

Output

Today is Sunday and Uri is happy
like image 131
YCF_L Avatar answered Apr 09 '26 15:04

YCF_L



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!