extract Name Ext - Java java.io

Java examples for java.io:File Extension Name

Description

extract Name Ext

Demo Code


//package com.java2s;

public class Main {

    public static String[] extractNameExt(String s) {
        int ind1 = s.lastIndexOf("/");
        if (ind1 < 0) {
            ind1 = 0;//from w  ww .  j  a  v a 2s.  c o  m
        } else {
            ind1 = ind1 + 1;
        }
        int ind2 = s.lastIndexOf(".");
        String name = "";
        String ext = "";
        if (ind2 < ind1) {
            ind2 = -1;
        }
        if (ind2 > 0) {
            name = s.substring(ind1, ind2);
            ext = s.substring(ind2 + 1, s.length());
        } else {
            name = s.substring(ind1);
        }
        return new String[] { name, ext };
    }
}

Related Tutorials