Java 如何从文件URL获取路径?
我有以下格式的字符串:Java 如何从文件URL获取路径?,java,regex,indexing,substring,Java,Regex,Indexing,Substring,我有以下格式的字符串: file://c:/Users/.... file://E:/Windows/.... file:///f:/temp/.... file:///H:/something/.... 我如何才能只获取c:/Users/..或H:/something/..?您可以将字符串中的字符串“file://”替换为空: String path = yourString.replace("file://", ""); 那怎么办 String path = yourString.rep
file://c:/Users/....
file://E:/Windows/....
file:///f:/temp/....
file:///H:/something/....
我如何才能只获取
c:/Users/..
或H:/something/..
?您可以将字符串中的字符串“file://”替换为空:
String path = yourString.replace("file://", "");
那怎么办
String path = yourString.replaceFirst("file:[/]*", "");
已测试并将替换任意数量的斜杠
String path = yourString.replaceFirst("file:/*", "");
如果你只想让它匹配两个或三个斜杠
String path = yourString.replaceFirst("file:/{2,3}", "");
如果它包含3个斜杠,比如
file://
,会怎么样?上面的答案不会满足您的要求file:///f:/temp/.... file:///H:/something/....String path=yourString.replaceFirst(“^file:///?”,”)代码>-没有方法String。替换(String,String)
@christoff-Hammarström:那file://c/Users
该[]
是冗余的。而且,//?
比/{2,3}
更短、更简单。
String path = new java.net.URI(fileUrl).getPath();