1

我想从“ https://rumpelstiltskin.com:7071/service/admin/soap ”返回“ https://rumpelstiltskin.com:7071/service/admin

我正在这样做:

String str="https://rumpelstiltskin.com:7071/service/admin/soap";
String strOut="";
strOut=str.split("/soap")[0];
System.out.println(strOut);

有没有更优雅的方法来做到这一点?

4

5 回答 5

2

使用以下内容怎么样String#replace()

String str = "https://rumpelstiltskin.com:7071/service/admin/soap";
String newString = str.replace("/soap", "");
System.out.println(newString);

输出是(经过试验和测试):

https://rumpelstiltskin.com:7071/service/admin
于 2013-09-16T18:55:33.170 回答
2

尝试这个

 String str="https://rumpelstiltskin.com:7071/service/admin/soap";
 String newStr = str.substring(0,str.lastIndexOf("/"));
 System.out.println(newStr);
于 2013-09-16T19:00:02.267 回答
1

你可以试试这个:-

 String str = "https://rumpelstiltskin.com:7071/service/admin/soap";
 String s = str.replace("/soap", "");
于 2013-09-16T18:56:38.967 回答
0
String str="https://rumpelstiltskin.com:7071/service/admin/soap";
String strOut= str.substring(0, str.lastIndexOf("/"));

仅在按照要求最后一个块始终为 /soap 时才有效

于 2013-09-16T19:01:33.270 回答
0
String str="https://rumpelstiltskin.com:7071/service/admin/soap";
String strOut=str.substring(0, str.lastIndexOf("/soap"));
System.out.println(strOut);
于 2013-09-16T19:12:15.797 回答