【问题标题】:How to replace a particular string with value in java如何用java中的值替换特定字符串
【发布时间】:2013-05-02 11:22:19
【问题描述】:

编辑:

目标:http://localhost:8080/api/upload/form/test/test

Is it possible to have some thing like `{a-b, A-B..0-9}` kind of pattern and match them and replace with value.

我有以下字符串

http://localhost:8080/api/upload/form/{uploadType}/{uploadName}

可以有任何字符串,例如{uploadType}/{uploadName}。

如何用java中的一些值替换它们?

【问题讨论】:

  • 创建一个名为“uploadName”的单独字符串并将其连接到您的父字符串
  • 它不是静态字符串,它是动态字符串,这就是我挣扎和眨眼的地方
  • 这个问题你已经问过了,为什么还要问? stackoverflow.com/questions/16335539/…
  • 动态到底是什么意思?您的意思是不会有固定数量的组件需要更换?
  • 感谢现在这个问题已经明白了

标签: java string


【解决方案1】:

[已编辑] 显然您不知道要寻找什么替代品,或者没有合理的有限地图。在这种情况下:

Pattern SUBST_Patt = Pattern.compile("\\{(\\w+)\\}");
StringBuilder sb = new StringBuilder( template);
Matcher m = SUBST_Patt.matcher( sb);
int index = 0;
while (m.find( index)) {
    String subst = m.group( 1);
    index = m.start();
    //
    String replacement = "replacement";       // .. lookup Subst -> Replacement here
    sb.replace( index, m.end(), replacement);
    index = index + replacement.length();
}

看,我现在真的期待 +1。


[更简单的方法]String.replace() 是一个“简单替换”且易于使用的方法;如果你想要正则表达式,你可以使用String.replaceAll()。

对于多个动态替换:

public String substituteStr (String template, Map<String,String> substs) {
    String result = template;
    for (Map.Entry<String,String> subst : substs.entrySet()) {
        String pattern = "{"+subst.getKey()+"}";
        result = result.replace( pattern, subst.getValue());
    }
    return result;
}

这是一种快速简便的方法。

【讨论】:

  • 我很抱歉问这个Map&lt;String,String&gt;会有什么内容
  • @Anto Maps 都有唯一的键和值。因此,由于键是第一个,即您要替换的那个,另一个是您要替换它的值。例如:Map
  • 如果我知道要替换什么键,我就不需要在这里问这个问题 Lyrion。对不起
  • 谢谢@Anto。我实际上在商业软件中使用过类似的技术。它很受客户欢迎。
  • 嗨,Thomas,这也是预期的答案,谢谢。托马斯请你给我你的电子邮件ID。
【解决方案2】:

您可以通过以下方式使用替换方法:

    String s = "http://localhost:8080/api/upload/form/{uploadType}/{uploadName}";
    String typevalue = "typeValue";
    String nameValue = "nameValue";
    s = s.replace("{uploadType}",value).replace("{uploadName}",nameValue);

【讨论】:

    【解决方案3】:

    你可以取从 {uploadType} 开始的字符串直到结束。 然后您可以使用“split”将该字符串拆分为字符串数组。 是否第一个单元格 (0) 是类型,1 是名称。

    【讨论】:

      【解决方案4】:

      解决方案 1:

      String uploadName = "xyz";
      String url = "http://localhost:8080/api/upload/form/" + uploadName;
      

      解决方案 2:

      String uploadName  = "xyz";
      String url = "http://localhost:8080/api/upload/form/{uploadName}";
      url.replace("{uploadName}",uploadName );
      

      解决方案 3:

      String uploadName  = "xyz";
      String url = String.format("http://localhost:8080/api/upload/form/ %s ", uploadName);
      

      【讨论】:

        【解决方案5】:
        String s = "http://localhost:8080/api/upload/form/{uploadType}/{uploadName}";
        String result = s.replace("uploadType", "UploadedType").replace("uploadName","UploadedName");
        

        编辑:试试这个:

        String r = s.substring(0 , s.indexOf("{")) + "replacement";
        

        【讨论】:

        • {uploadType}/{uploadName} 这可以是任何字符串,但常见的是 {} 这就是我闪烁的原因
        • 那不是静态字符串,是动态字符串。看看我的评论和编辑,谢谢你的帖子
        • @Anto 使用 indexOf() 查看我的答案。
        • 这将替换所有内容,并且对于给定的示例字符串将保持一个值,例如 http://localhost:8080/api/upload/form/replacement
        【解决方案6】:

        UriBuilder 完全满足您的需求:

        UriBuilder.fromPath("http://localhost:8080/api/upload/form/{uploadType}/{uploadName}").build("foo", "bar");
        

        结果:

        http://localhost:8080/api/upload/form/foo/bar
        

        【讨论】:

        • 我已经评论说它不是静态字符串和动态的:(
        • 如果你的意思是没有固定数量的参数要添加,那不是问题。如果它是动态的,那么无论如何您都将迭代地替换组件,请查看链接中的 UriBuilder 方法,并查看如何将 URI 动态组合在一起,然后再假设此解决方案仅适用于静态字符串和 downvoting - 这是有原因的当你几分钟前问这个问题时,这得到了 3 票。
        猜你喜欢
        • 2016-11-20
        • 1970-01-01
        • 2018-10-16
        • 1970-01-01
        • 2019-04-12
        • 1970-01-01
        • 1970-01-01
        • 2011-01-07
        • 1970-01-01
        相关资源
        最近更新 更多