2013-05-02 141 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