【发布时间】:2011-11-06 15:24:45
【问题描述】:
我有这种格式的字符串:
file://c:/Users/....
file://E:/Windows/....
file:///f:/temp/....
file:///H:/something/....
我怎样才能只得到c:/Users/... 或H:/something/... ?
【问题讨论】:
标签: java regex indexing substring
我有这种格式的字符串:
file://c:/Users/....
file://E:/Windows/....
file:///f:/temp/....
file:///H:/something/....
我怎样才能只得到c:/Users/... 或H:/something/... ?
【问题讨论】:
标签: java regex indexing substring
经过测试,将替换任意数量的斜线。
String path = yourString.replaceFirst("file:/*", "");
如果你只希望它匹配两个或三个斜杠
String path = yourString.replaceFirst("file:/{2,3}", "");
【讨论】:
[] 是多余的。此外,///? 比 /{2,3} 更短更简单。
String path = new java.net.URI(fileUrl).getPath();
【讨论】:
您可以将字符串中的字符串“file://”替换为空:
String path = yourString.replace("file://", "");
【讨论】:
file:/// 这样的斜线呢?
String path = yourString.replaceFirst("^file:///?", ""); - 没有方法String.replace(String, String)。
file://c/Users
那又怎样?
String path = yourString.replaceFirst("file:[/]*", "");
【讨论】: