Extract a partial name of a class name, starting from the end - Android java.lang

Android examples for java.lang:Class

Description

Extract a partial name of a class name, starting from the end

Demo Code


public class Main{

    /**/*from  w  w w  .  j  av  a 2s .c  o m*/
     * Extract a partial name of a class name, starting from the end.
     * 
     * @param string
     *            the name of the class
     * @param parts
     *            the number of parts of the class name that you want to be
     *            returned.
     * 
     * @return the partial class name.
     */
    public static String extractPartialClassName(String className, int parts) {
        String partialCategoryName = className;

        int nofDots = 0;
        int dotIndex = className.lastIndexOf('.');
        if (dotIndex != -1) {
            nofDots++;
        }

        while (nofDots < parts && dotIndex > -1) {
            dotIndex = className.lastIndexOf('.', dotIndex - 1);

            if (dotIndex != -1) {
                nofDots++;
            }
        }

        if (dotIndex > -1 && nofDots <= parts) {
            partialCategoryName = className.substring(dotIndex + 1);
        }

        return partialCategoryName;
    }

}

Related Tutorials