2013-05-02 32 views
0

编辑:如何用java值替换特定字符串

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

+0

创建一个独立的字符串名称'uploadName”,它串联到父串 – gauravsapiens 2013-05-02 11:24:01

+0

它不是静态的字符串,它是动态的字符串,那就是我在挣扎和眨眼的地方 – 2013-05-02 11:26:21

+0

你已经问过这个问题,你为什么再问一次? http://stackoverflow.com/questions/16335539/java-split-and-replace-a-string-with-value/16335575#16335575 – Quetzalcoatl 2013-05-02 11:31:06

回答

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

这是快速&简单的方法,开始。

+0

我很抱歉问这个'Map ' – 2013-05-02 11:39:46

+1

@Anto Maps都有独特的键和值附加到它。所以,因为关键是第一个,那就是你想要替换的那个,另一个是你想要替换的值。例如:Map Lyrion 2013-05-02 11:49:59

+0

如果我知道替换的关键是什么,我不需要在这里问这个问题Lyrion。对不起 – 2013-05-02 11:51:47

0

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

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

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

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

'{uploadType}/{uploadName}'这可以anystring,但常见的是'{}'这就是为什么我闪烁 – 2013-05-02 11:28:30

+0

这不是一个静态字符串,它是一个动态字符串。看到我的评论和编辑,谢谢你的帖子 – 2013-05-02 11:31:12

+0

@Anto使用indexOf()查看我的答案。 – 2013-05-02 11:34:21

0

解决方案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); 
-1

UriBuilder不正是你所需要的:在

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

结果:

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

我已经评论过它不是静态字符串和动态:( – 2013-05-02 11:33:43

+0

如果你的意思是说没有固定数量的参数要添加,那不是问题,如果它是动态的,那么你将会迭代地更换组件,查看UriBuilder方法链接,看看如何在动态地将URI放在一起之前,假设这个解决方案只适用于静态字符串和downvoting - 当你在几分钟前提出这个问题时,有3个upvotes的原因。 – Quetzalcoatl 2013-05-02 11:37:42

相关问题