Java拆分并用值替换字符串
我有一个字符串,我必须在某些条件下拆分并替换该值Java拆分并用值替换字符串,java,string,split,Java,String,Split,我有一个字符串,我必须在某些条件下拆分并替换该值 http://localhost:8080/api/upload/form/{uploadType}/{uploadName} 其中我必须只替换{uploadType}/{uploadName}的值。我真的不知道如何用价值来取代它们 {uploadType}/{uploadName}中的字符串可以是任何要替换的类型 我尝试过以下方法: 包com.test.poc public class TestString { public static v
http://localhost:8080/api/upload/form/{uploadType}/{uploadName}
其中我必须只替换{uploadType}/{uploadName}的值。我真的不知道如何用价值来取代它们
{uploadType}/{uploadName}
中的字符串可以是任何要替换的类型
我尝试过以下方法:
包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)
但我有以下例外:
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)
有人能帮我一下吗?A
{
是一个regex元字符,用于范围重复
在那之前
toBeFixed = toBeFixed.replaceAll("\\{", "\n");
另见文件:
类似的问题:A{
是一个regex元字符,用于范围重复
在那之前
toBeFixed = toBeFixed.replaceAll("\\{", "\n");
另见文件:
类似的问题:也许
(碰巧使用了您正在使用的确切格式)可能是
(恰好使用了您正在使用的确切格式)您可以尝试以下方法:
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);
}
}
}
你可以试试这个:
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);
}
}
}
如果您使用SpringMVC,此功能是现成的
(事实上,即使不使用Spring MVC,您仍然可以使用此技术,但显然,您需要在类路径上安装Spring MVC jar)如果您使用Spring MVC,此功能是现成的
(事实上,即使不使用Spring MVC,您仍然可以使用此技术,但显然,您需要在类路径上使用Spring MVC jar)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('{','')和String.replace('}','')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('{','')和String.replace('}','')在字符串上要替换的参数数量将仅在运行时扣除,UriBuilder
是一个生成器模式,您可以使用其返回自身的方便方法迭代地向其添加组件。它与您的情况完全相同,比regex干净得多。要替换的参数数量将仅在运行时扣除由于UriBuilder
是一种生成器模式,您可以使用其返回自身的方便方法迭代地向其添加组件。它与您的情况完全相同,比正则表达式干净得多。要替换的参数数量将仅在运行时扣除要替换的参数数量被替换的将仅在运行时扣除http://localhost:8080/api/upload/form/ uploadType}/uploadName}
是输出,如果我遵循您的代码,如何用valuehttp://localhost:8080/api/upload/form/ uploadType}/uploadName}
是我按照你的代码做的输出,如何用值替换这两个变量?我已经按照您的输入进行了操作,但它给了我java.lang.IllegalArgumentException:没有足够的变量值可用于展开“uploadName”
将更新问题now@Anto只有当您知道编译时的参数数量时,此方法才起作用。我跟踪了您的输入,但它给出了java.lang.IllegalArgumentException:没有足够的变量值可用于展开“uploadName”
将更新问题now@Anto只有在编译时知道参数的数量时,此方法才有效