【问题标题】:Replacing contents inside curly braces (for e.g. {1}) with something else [duplicate]用其他东西替换花括号内的内容(例如{1})[重复]
【发布时间】:2014-08-07 00:46:40
【问题描述】:

我有一个字符串如下

Hey {1}, you are {2}.

这里12 是键,其值将被动态添加。

现在我需要将{1} 替换为1 代表的值,然后我需要将{2} 替换为上面句子中2 代表的值。

我该怎么做?

我知道字符串的拆分函数是做什么的,我非常清楚使用该函数我可以做我想做的事,但我正在寻找更好的东西。

注意:我事先不知道这些键是什么。我还需要检索密钥。然后根据我需要替换字符串中的值的键。

【问题讨论】:

  • 不错的问题.. 为什么投反对票?
  • @Smrita 您想从您的字符串中替换 {1}{2} 并且您的字符串中只有两个值要替换不超过 2 是正确的..
  • 不是我会有很多值:)。
  • 所以这意味着你的字符串也看起来像这样Hey {1} , you are {2} and you can {3} with {4}等等......你必须替换大括号中的所有值
  • @amitbhardwaj 是的,这就是我的情况:)。

标签: java string


【解决方案1】:

您可以使用 java.text.MessageFormat 中的 MessageFormatMessage Format 有一些关于如何在此类场景中使用它的示例

【讨论】:

    【解决方案2】:

    感谢https://stackoverflow.com/users/548225/anubhava 的这个.. :)。你可以这样做:

    public static void main(String[] args) {
        String s = "Hey {1}, you are {2}.";
        HashMap<Integer, String> hm = new HashMap();
        hm.put(1, "one");
        hm.put(2, "two");
        Pattern p = Pattern.compile("(\\{\\d+\\})");
        Matcher m = p.matcher(s);
        while (m.find()) {
            System.out.println(m.group());
            String val1 = m.group().replace("{", "").replace("}", "");
            System.out.println(val1);
            s = (s.replace(m.group(), hm.get(Integer.parseInt(val1))));
            System.out.println(s);
        }
    
    }
    

    输出:

    Hey one, you are two.
    

    【讨论】:

    • 谢谢它的工作:)。 TheLostMind 摇滚:)
    • @Smrita - 祝你好运尝试修改超过 2 个值:P
    • 这个想法很好,但匹配和替换应该在while (m.find()) {...}循环中使用简单的正则表达式\\{(\\d+)\\}
    • @anubhava - 是的..我同意..我只是把问题复杂化了..并且急于发布解决方案:P
    • @anubhava - 改变了它.. 谢谢 :)
    【解决方案3】:

    尝试使用String.format()

    String x = String.format("Hi %s, you are %s\n", str1, str2);
    

    如果您已经有一个字符串"Hey {1}, you are {2}.",您可以使用正则表达式将{1}{2} 替换为%s

    String template = "Hey {1}, you are {2}.";
    String x = String.format(template.replaceAll("\\{\\d\\}", "%s"), "Name", "Surname");
    

    【讨论】:

    • 我怕用不了!
    • @Smrita 你不能使用格式吗?
    【解决方案4】:

    试试这个,我认为这会根据我从您的问题中理解的内容起作用

    public static void main(String[] args) {
           String str = "Hey {1} your {2} is {3}  clear to {4}  ";
           Map<Integer,String> map = new LinkedHashMap<Integer,String>();
           map.put(1, "Smrita");
           map.put(2, "question");
           map.put(3, "not");
           map.put(4, "anyone");
           while(str.contains("{")){
               Integer i = Integer.parseInt(str.substring(str.indexOf("{")+1, str.indexOf("{")+2));
               str = str.replace("{"+i+"}", map.get(i));
    
           }
            System.out.println(str);
        }
    

    输出是

    Hey Smrita your question is not clear to anyone

    【讨论】:

    • aah.. 我们中的一些人总是倾向于忽略简单之美。 :)。 +1
    【解决方案5】:

    试试这个:

    String str = "Hey {1}, you are {2}.";
    str = str.replaceFirst("\\{.*?\\}", "Matt");
    

    输出:

    Hey Matt, you are {2}.
    

    上面的代码将替换{1} 的第一个实例。如果您在循环中调用此序列并替换代码中的键值,它将按照给定字符串中提到的顺序替换{...} 中的所有字符串。

    【讨论】:

      猜你喜欢
      • 2019-07-20
      • 2013-12-02
      • 1970-01-01
      • 1970-01-01
      • 2016-07-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多