Android File Name Get getNamePart(String fileName)

Here you can find the source of getNamePart(String fileName)

Description

get Name Part

Declaration

public static String getNamePart(String fileName) 

Method Source Code

//package com.java2s;

public class Main {

    public static String getNamePart(String fileName) {
        int point = getPathLastIndex(fileName);
        int length = fileName.length();
        if (point == -1) {
            return fileName;
        } else if (point == length - 1) {
            int secondPoint = getPathLastIndex(fileName, point - 1);
            if (secondPoint == -1) {
                if (length == 1) {
                    return fileName;
                } else {
                    return fileName.substring(0, point);
                }//from w ww  .  j a v a 2 s.  c  o  m
            } else {
                return fileName.substring(secondPoint + 1, point);
            }
        } else {
            return fileName.substring(point + 1);
        }
    }

    public static int getPathLastIndex(String fileName) {
        int point = fileName.lastIndexOf('/');
        if (point == -1) {
            point = fileName.lastIndexOf('\\');
        }
        return point;
    }

    public static int getPathLastIndex(String fileName, int fromIndex) {
        int point = fileName.lastIndexOf('/', fromIndex);
        if (point == -1) {
            point = fileName.lastIndexOf('\\', fromIndex);
        }
        return point;
    }
}

Related

  1. getFileName(String path)
  2. getRandomFileName(String prefix, String suffix)
  3. parseFileName(String path)
  4. getSuffix(File file)
  5. getSuffix(String fileName)
  6. appendNumberToFilenameIfExists(String fileName, File directory)
  7. getUniqueImageFilename(String prefix)
  8. getFileName()
  9. getFileName(String path)