2013-05-02 80 views
0

替換字符串我有一個下面的字符串,我有分裂和一些條件替換值爪哇分裂並與值

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

,其中我只更換爲{uploadType}的值/ { uploadName}。我真的不知道如何去取代它們的價值。

{uploadType}/{uploadName}中的字符串可以是任何類型的字符串。

我試過類似下面的東西:

package com.test.poc;

public class TestString { 
public static void main(String[] args) throws ClassNotFoundException { 
    String toBeFixed = "http://localhost:8080/api/upload/form/{uploadType}/{uploadName}"; 
    String[] toReplaceWith =toBeFixed.split("{"); 
for (String string : toReplaceWith) { 
    System.out.println("string : "+string); 
} 
} 

} 

,但我得到以下異常:

Exception in thread "main" java.util.regex.PatternSyntaxException: Illegal repetition 
{ 
    at java.util.regex.Pattern.error(Pattern.java:1713) 
    at java.util.regex.Pattern.closure(Pattern.java:2775) 
    at java.util.regex.Pattern.sequence(Pattern.java:1889) 
    at java.util.regex.Pattern.expr(Pattern.java:1752) 
    at java.util.regex.Pattern.compile(Pattern.java:1460) 
    at java.util.regex.Pattern.<init>(Pattern.java:1133) 
    at java.util.regex.Pattern.compile(Pattern.java:823) 
    at java.lang.String.split(String.java:2292) 
    at java.lang.String.split(String.java:2334) 
    at com.test.poc.TestString.main(TestString.java:9) 

編輯:

這是我嘗試基於Sean Patrick Floyd答案

public String doPOSTPathVariable(String uri,List paramsList) throws IllegalArgumentException, IllegalAccessException, InstantiationException, Exception{ 
     String uriString=""; 
     UriComponents uriComponents = UriComponentsBuilder.fromUriString(uri).build(); 
     for (int j = 0; j<= paramsList.size(); j++) { 
      System.out.println("path variable"); 
      MethodParams methodParams; 
      methodParams =(MethodParams) paramsList.get(j); 

      if(methodParams.isPrimitive() && methodParams.getDataType()=="boolean"){ 
        uriString = uriComponents.expand(true).encode().toUriString(); 
      } 
      if(methodParams.isPrimitive() && methodParams.getDataType()=="java.math.BigDecimal"){ 
       uriString = uriComponents.expand(123).encode().toUriString(); 
      } 
      if(methodParams.isPrimitive() && methodParams.getDataType()=="java.lang.String"){ 
       uriString = uriComponents.expand("hexgen").encode().toUriString(); 
      } 
     } 
     return uriString; 
    } 

的方法,但我得到以下異常:

java.lang.IllegalArgumentException: Not enough variable values available to expand 'uploadName' 
    at org.springframework.web.util.UriComponents$VarArgsTemplateVariables.getValue(UriComponents.java:1025) 
    at org.springframework.web.util.UriComponents.expandUriComponent(UriComponents.java:443) 
    at org.springframework.web.util.UriComponents.access$1(UriComponents.java:431) 
    at org.springframework.web.util.UriComponents$FullPathComponent.expand(UriComponents.java:800) 
    at org.springframework.web.util.UriComponents.expandInternal(UriComponents.java:413) 
    at org.springframework.web.util.UriComponents.expand(UriComponents.java:404) 
    at com.hexgen.tools.HexgenClassUtils.doPOSTPathVariable(HexgenClassUtils.java:208) 
    at com.hexgen.reflection.HttpClientRequests.handleHTTPRequest(HttpClientRequests.java:77) 
    at com.hexgen.reflection.HexgenWebAPITest.main(HexgenWebAPITest.java:115) 

請問有人能幫我嗎?

+0

要替換的參數no將在運行時僅扣除 – 2013-05-02 10:26:23

回答

2

也許UriBuilder這樣做

toBeFixed = toBeFixed.replaceAll("\\{", "\n"); 

看到文檔之前也

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

(便利恰巧使用您正在使用的精確格式)

+0

要替換的參數no只會在運行時被扣除 – 2013-05-02 10:25:32

+0

'UriBuilder'是一個構建器模式,你可以使用其自身返回的便利方法迭代地向其添加組件。它的存在與你的情況完全一樣,比正則表達式更清潔。 – Quetzalcoatl 2013-05-02 10:28:42

1

你可以試試這個:

public class TestString { 
public static void main(String[] args) throws ClassNotFoundException { 
String toBeFixed = "http://localhost:8080/api/upload/form/{uploadType}/{uploadName}"; 
String[] toReplaceWith =toBeFixed.split("\\{"); 
for (String string : toReplaceWith) { 
System.out.println("string : "+string); 
} 
} 

} 
2

如果您使用Spring MVC,此功能可開箱with the new URI builder technology

UriComponents uriComponents = 
    UriComponentsBuilder.fromUriString(
     "http://example.com/hotels/{hotel}/bookings/{booking}").build(); 

URI uri = uriComponents.expand("42", "21").encode().toUri(); 
// or: 
String uriString = uriComponents.expand("42", "21").encode().toUriString(); 

(事實上,你仍然可以使用這個技術,即使你不使用Spring MVC的,但你必須在類路徑中的Spring MVC罐子,很明顯)

+0

我跟着你的輸入,但它給了我'java.lang.IllegalArgumentException:沒有足夠的變量值可用來擴大'uploadName''會更新現在的問題 – 2013-05-02 11:12:56

+0

請看看我編輯的問題 – 2013-05-02 11:15:13

+0

@Anto此方法只適用於你知道編譯時參數的數量 – 2013-05-02 11:31:13

1

UrlBuilder是該解決方案,但如果你只是環顧四周分隔符,你也可以使用字符串:

String url = "http://localhost:8080/api/upload/form/{uploadType}/{uploadName}"; 
int lastSlash = url.lastIndexOf('/'); 
int secondLastSlash = url.substring(0, lastSlash).lastIndexOf('/'); 
System.out.println(url.substring(secondLastSlash+1, lastSlash)); 
System.out.println(url.substring(lastSlash+1)); 

要刪除的大括號,你可以用與string.replace(「{」,「」)和字符串。替換('}','')字符串