简体   繁体   English

如何用Java中的值替换特定的字符串

[英]How to replace a particular string with value in java

EDIT : 编辑:

Goal : http://localhost:8080/api/upload/form/test/test 目标: 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.

i have following string 我有以下字符串

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

there can be any no of strings like {uploadType}/{uploadName} . 不能有任何字符串,例如{uploadType}/{uploadName}

how to replace them with some values in java? 如何用Java中的某些值替换它们?

[Edited] Apparently you don't know what substitutions you'll be looking for, or don't have a reasonable finite Map of them. [编辑]显然,您不知道要寻找什么替代品,或者没有合理的有限映射。 In this case: 在这种情况下:

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();
}

Look, I'm really expecting a +1 now. 看,我真的很期待+1。


[Simpler approach] String.replace() is a 'simple replace' & easy to use for your purposes; [更简单的方法] String.replace()是一个“简单替换”并且易于使用。 if you want regexes you can use String.replaceAll() . 如果需要正则表达式,可以使用String.replaceAll()

For multiple dynamic replacements: 对于多个动态替换:

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;
}

That's the quick & easy approach, to start with. 首先,这是快速简便的方法。

You can use the replace method in the following way: 您可以通过以下方式使用replace方法:

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

You can take the string that start from {uploadType} till the end. 您可以采用从{uploadType}到结尾的字符串。 Then you can split that string using "split" into string array. 然后,您可以使用“ split”将字符串拆分为字符串数组。 Were the first cell(0) is the type and 1 is the name. 第一个单元格(0)是类型,而1是名称。

String s = "http://localhost:8080/api/upload/form/{uploadType}/{uploadName}";
String result = s.replace("uploadType", "UploadedType").replace("uploadName","UploadedName");

EDIT: Try this: 编辑:试试这个:

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

Solution 1 : 解决方案1:

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

Solution 2: 解决方案2:

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

Solution 3: 解决方案3:

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

The UriBuilder does exactly what you need: UriBuilder完全UriBuilder您的需求:

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

Results in: 结果是:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM