在Java中,给定a java.net.URL
或a String
形式的http://www.example.com/some/path/to/a/file.xml
,减去扩展名的最简单方法是什么?因此,在此示例中,我正在寻找可返回的东西"file"
。
我可以想到几种方法来实现此目的,但是我正在寻找易于阅读且简短的内容。
与其重新发明轮子,不如使用Apache commons-io:
import org.apache.commons.io.FilenameUtils;
public class FilenameUtilTest {
public static void main(String[] args) throws Exception {
URL url = new URL("http://www.example.com/some/path/to/a/file.xml?foo=bar#test");
System.out.println(FilenameUtils.getBaseName(url.getPath())); // -> file
System.out.println(FilenameUtils.getExtension(url.getPath())); // -> xml
System.out.println(FilenameUtils.getName(url.getPath())); // -> file.xml
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句