List of utility methods to do File Name Extract
String | extractFileName(String path) extract File Name int index = path.lastIndexOf("\\"); if (index > -1) { if (index < path.length() - 1) { return path.substring(index + 1); } else { return ""; index = path.lastIndexOf("/"); if (index > -1) { if (index < path.length() - 1) { return path.substring(index + 1); } else { return ""; return path; |
String | extractFileName(String path) Pass in /sub/dir/path.html returns path.html if (path == null) { return null; String newpath = path.replace('\\', '/'); int start = newpath.lastIndexOf("/"); if (start == -1) { start = 0; } else { ... |
String | extractFileName(String path) Extracts filename portion out of supplied pathname (leaves last segment only) if (path == null || path.isEmpty()) return path; int pos = path.lastIndexOf('/'); if (pos < 0) pos = path.lastIndexOf('\\'); if (pos >= 0) return path.substring(pos + 1); return path; ... |
String | extractFilename(String path) Extract the filename from a (relative or absolute) path. String result = path.substring(path.lastIndexOf('/') + 1); return result.substring(result.lastIndexOf('\\') + 1); |
String | extractFileName(String url) Extract the file name from the URL removing the scheme, domain, query params and named anchor, if present. int index1 = url.indexOf('?'); int index2 = url.indexOf('#'); if (index1 == -1) { index1 = url.length() + 1; if (index2 == -1) { index2 = url.length() + 1; int index = Math.min(index1, index2); if (index < url.length()) { url = url.substring(0, index); index1 = url.lastIndexOf('/'); index2 = url.lastIndexOf('\\'); index = Math.max(index1, index2); url = url.substring(index + 1); return url; |
String | extractFileNameFromBAMLocation(String location) Converts strings like /data/results/DNA1234049.bam or DNA1234049.bam to DNA1234049 String fileName = ""; location = location.toLowerCase(); String[] split = location.split("/"); for (String s : split) { if (s.endsWith(".bam")) { fileName = s.replaceAll(".bam$", ""); return fileName; |
String | extractFileNameFromContentDisposition(String contentDisposition) Extract the file name from the content disposition header. System.out.println("content disposition = " + contentDisposition); String[] attributes = contentDisposition.split(";"); for (String a : attributes) { if (a.toLowerCase().contains("filename")) { try { return a.substring(a.indexOf('\"') + 1, a.lastIndexOf('\"')); } catch (Exception e) { return a.substring(a.indexOf('=') + 1, a.length()); ... |
String | extractFileNameFromPath(final String filePath, char separatorChar) extract File Name From Path int index = filePath.lastIndexOf(separatorChar); if (index < 0) { return filePath; return filePath.substring(index + 1); |
String | extractFileNameFromPath(final String path) Returns the file name from the path Never null
final int lastIndex = path.lastIndexOf('/'); if (lastIndex == -1) { throw new IllegalArgumentException("The specified path '" + path + "' doesn't contain '/'"); } else { return path.substring(lastIndex + 1, path.length()); |
String | extractFilenameFromPath(String path) Returns only file name from path String sTmp = path.replaceAll("\\\\", "/"); int lastPos = sTmp.lastIndexOf('/'); if (lastPos >= 0) return sTmp.substring(lastPos + 1); else return sTmp; |