[英]Java 8 Get full URL path without last segment (with or without trailing slash)
I have URLs as below and need to trim them as follows without the last segment. 我有如下的URL,需要修剪它们如下,没有最后一段。 There may or may not be a trailing slash. 可能有也可能没有尾随斜线。
localhost:8080/myapp
-> localhost:8080/
localhost:8080/myapp
- > localhost:8080/
https://myapp-dev.myhost.com/app/
-> https://myapp-dev.myhost.com/
https://myapp-dev.myhost.com/app/
- > https://myapp-dev.myhost.com/
https://myapp-dev.myhost.com/app/app2
-> https://myapp-dev.myhost.com/app/
https://myapp-dev.myhost.com/app/app2
- > https://myapp-dev.myhost.com/app/
Of course I could try solutions like 当然我可以试试像
String[] tokens = uri.split("/"); // then concatenate previous ones...
or 要么
Path path = Paths.get(uri.getPath());
String secondToLast = path.getName(path.getNameCount() - 2).toString();
But isn't there some more robust utility or method? 但是,有没有更强大的实用工具或方法?
Try passing the url string into a URL object and then pulling out the required segments: 尝试将url字符串传递给URL对象,然后拉出所需的段:
URL someURL = new URL("https://myapp-dev.myhost.com/app/");
System.out.println("PATH = " + someURL.getPath());
System.out.println("HOST = " + someURL.getHost());
System.out.println("PROTOCOL = " + someURL.getProtocol());
System.out.println("PORT = " + someURL.getPort());
output: 输出:
PATH = /app/ 路径= / app /
HOST = myapp-dev.myhost.com HOST = myapp-dev.myhost.com
PROTOCOL = https PROTOCOL = https
PORT = 8080 PORT = 8080
If all you need is to trim everything after the last "/" (or the second last if the string ends with "/") may be a simple function could solve this: 如果您需要的是在最后一个“/”之后修剪所有内容(或者如果字符串以“/”结尾则是倒数第二个)可能是一个简单的函数可以解决这个问题:
public static void main(String[] args){
Function<String,String> trimUrlString = s -> {
s = s.endsWith("/") ? s.substring(0, s.length()-1) : s;
return s.substring(0, s.lastIndexOf('/')+1);
};
String u1 = "localhost:8080/myapp";
System.out.println(trimUrlString.apply(u1));
String u2 = "https://myapp-dev.myhost.com/app/";
System.out.println(trimUrlString.apply(u2));
}
//output: localhost:8080/ https://myapp-dev.myhost.com/
EDIT 编辑
Another aproach which might be shorter is to chain two replaceAll calls : 另一个可能更短的方法是链接两个replaceAll调用:
myString.replaceAll("/$", "").replaceAll("/[^/]+$", "/");
The first call will remove a forward slash at the end if any, if there is no slash at the end myString
remains the same. 第一个调用将删除最后的正斜杠(如果有的话),如果最后没有斜杠, myString
保持不变。 The second call will then replace every char after the last /
which is not a /
然后,第二个电话会取代后,最后每一个字符/
这不是/
Some test cases with your examples: 您的示例中的一些测试用例:
String[] urls = {"localhost:8080/myapp",
"https://myapp-dev.myhost.com/app/test.pdf",
"http://myapp-dev.host.com/app/",
"http://app.host.com:8080/app/app2"};
for(String url : urls){
String s = url.replaceAll("/$", "").replaceAll("/[^/]+$", "/");
System.out.println(url);
System.out.println(s);
System.out.println();
}
You can split the string using a regular expression as I have mentioned in the comment. 您可以使用正则表达式拆分字符串,如我在评论中提到的那样。 I provide below the Regex. 我在正则表达式下面提供。
^https?:\/\/\w+(:[0-9]*)?(\.\w+)?
You can try with the following examples. 您可以尝试使用以下示例。
https://mydomain:8080 https://开头MYDOMAIN:8080
http://localhost:8090 HTTP://本地主机:8090
You can also verify in https://rubular.com/ by pasting the regular expression and the example strings. 您还可以通过粘贴正则表达式和示例字符串在https://rubular.com/中进行验证。
Use String.lastIndexOf
and String.substring
to strip off last component. 使用String.lastIndexOf
和String.substring
去除最后一个组件。
Something like: 就像是:
private String stripLastComponent(String path) {
int n = path.lastIndexOf('/');
if(n < 0) { // no / in path
return path;
}
String stripped = path.substring(0, n);
if(n == path.length()) { // '/' was last char, so try stripping again
stripped = stripLastComponent(stripped);
}
return stripped;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.