如何在java中用值替换特定字符串

如何在java中用值替换特定字符串,java,string,Java,String,编辑: 目标: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} 不能有任何字符串

编辑:

目标:
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中的某些值替换它们?

您可以按以下方式使用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);

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

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

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

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();
}
听着,我现在真的很期待a+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;
}
publicstringsubstitutest(字符串模板、映射替换){
字符串结果=模板;
对于(Map.Entry subst:subss.entrySet()){
字符串模式=“{”+subst.getKey()+“}”;
result=result.replace(pattern,subst.getValue());
}
返回结果;
}

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

解决方案1:

String uploadName = "xyz";
String url = "http://localhost:8080/api/upload/form/" + uploadName;
String uploadName  = "xyz";
String url = "http://localhost:8080/api/upload/form/{uploadName}";
url.replace("{uploadName}",uploadName );
String uploadName  = "xyz";
String url = String.format("http://localhost:8080/api/upload/form/ %s ", uploadName);
解决方案2:

String uploadName = "xyz";
String url = "http://localhost:8080/api/upload/form/" + uploadName;
String uploadName  = "xyz";
String url = "http://localhost:8080/api/upload/form/{uploadName}";
url.replace("{uploadName}",uploadName );
String uploadName  = "xyz";
String url = String.format("http://localhost:8080/api/upload/form/ %s ", uploadName);
解决方案3:

String uploadName = "xyz";
String url = "http://localhost:8080/api/upload/form/" + uploadName;
String uploadName  = "xyz";
String url = "http://localhost:8080/api/upload/form/{uploadName}";
url.replace("{uploadName}",uploadName );
String uploadName  = "xyz";
String url = String.format("http://localhost:8080/api/upload/form/ %s ", uploadName);
这正是您所需要的:

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

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

制作一个名为“uploadName”的单独字符串,并将其连接到父字符串。这不是静态字符串,而是动态字符串,这就是我正在努力的地方,并在哪里开始闪烁。你已经问过这个问题,为什么还要再问?你说的动态到底是什么意思?你的意思是不会有固定数量的组件需要更换吗?感谢大家现在才清楚地理解这个问题
{uploadType}/{uploadName}
这可以是任意字符串,但常见的是
{}
,这就是为什么我在闪烁这不是静态字符串,而是动态字符串。请参阅我的评论和编辑,谢谢您的支持post@Anto使用indexOf()查看我的答案。这将替换所有内容并保留一个值,如
http://localhost:8080/api/upload/form/replacement
对于给定的示例stringi,我已经指出它不是静态字符串和动态字符串:(如果您的意思是说没有固定数量的参数要添加,这不是问题。如果是动态的,那么您将以迭代方式替换组件,请查看链接中的
UriBuilder
方法,并了解如何在假设此解决方案仅适用于静态组件之前动态地将URI组合在一起字符串和向下投票-这是一个原因,当你几分钟前问这个问题时,它得到了3张向上投票。我很抱歉地问,这个
地图中会有什么内容
@Anto地图都有唯一的键和附加的值。因此,由于键是第一个,所以这是你要替换的键,另一个是你要用.F替换它的值或者示例:Mapif i i know which key to replace,即我不需要在这里问这个问题Lyrion。我很抱歉感谢@Anto。我实际上在商业软件中使用了类似的技术。这在客户中很流行。嗨,托马斯,这也是预期的答案,谢谢。托马斯,请给我你的电子邮件id。