Here you can find the source of baseName(String fn)
Parameter | Description |
---|---|
fn | the String filename to scan |
public static String baseName(String fn)
//package com.java2s; /*//from w ww . ja va2s.com * THIS SOURCE FILE IS PART OF JBASIC, AN OPEN SOURCE PUBLICLY AVAILABLE * JAVA SOFTWARE PACKAGE HOSTED BY SOURCEFORGE.NET * * THIS SOFTWARE IS PROVIDED VIA THE GNU PUBLIC LICENSE AND IS FREELY * AVAILABLE FOR ANY PURPOSE COMMERCIAL OR OTHERWISE AS LONG AS THE AUTHORSHIP * AND COPYRIGHT INFORMATION IS RETAINED INTACT AND APPROPRIATELY VISIBLE * TO THE END USER. * * SEE THE PROJECT FILE AT HTTP://WWW.SOURCEFORGE.NET/PROJECTS/JBASIC FOR * MORE INFORMATION. * * COPYRIGHT 2003-2011 BY TOM COLE, TOMCOLE@USERS.SF.NET * * Created on Jun 23, 2007 by tom * */ public class Main { /** * Given a full or partial path name, locate the "base name" from * the name. This means removing all absolute or relative path * name prefixes and any extension suffix. * @param fn the String filename to scan * @return the base name element of the string. If the path name * does not include a basename element (for example, consists only of a * directory specification) then an empty string is returned. */ public static String baseName(String fn) { /* * Start at the end of the string and search for a path * separator character. */ String separator = System.getProperty("file.separator"); int sepLen = separator.length(); int startPos = fn.length() - sepLen; if (startPos < 0) return ""; int pos; for (pos = startPos; pos >= 0; pos--) { String testChar = fn.substring(pos, pos + sepLen); if (testChar.equals(separator)) break; if (testChar.equals(":")) break; } if (pos == startPos) return ""; /* * Make a new copy of the part of the string that has only * the filename. If we never found a separator, adjust to * the start of the string. If we did find a separator, then * don't include it in the new filename string. */ if (pos < 0) pos = 0; else pos += 1; String fileName = fn.substring(pos); /* * Strip off the last part of the filename (the "extension") * if there is one, identified by the "dot". */ for (pos = fileName.length() - 1; pos >= 0; pos--) if (fileName.charAt(pos) == '.') break; if (pos < 0) pos = fileName.length(); return fileName.substring(0, pos); } }