2013-05-02 83 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(“{”,“”)和字符串。替换('}','')字符串