gpt4 book ai didi

java - 使用参数更新字符串

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:33:20 25 4
gpt4 key购买 nike

我在 Java 8 中使用 Spring。

我有一个带参数的字符串,我不确定应该如何通过参数列表更新参数。

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

我想知道我应该使用哪个运算符来获得:

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

谢谢

最佳答案

您可以使用 MessageFormat像这样:

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

输出

Today is Sunday and Uri is happy

关于java - 使用参数更新字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49160682/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com