Thursday 17 March 2016

Java Tutorial : Java String (filename split)


Click here to watch in Youtube :
https://www.youtube.com/watch?v=0qarnfWz3FQ&list=UUhwKlOVR041tngjerWxVccw

Click the below Image to Enlarge
Java Tutorial : Java String (filename split) 
Filename.java
public class Filename
{
    private String fullPath;
    private char pathSeparator, extensionSeparator;

    public Filename(String str, char sep, char ext)
    {
        fullPath = str;
        pathSeparator = sep;
        extensionSeparator = ext;
    }

    /*
     * Extension method uses lastIndexOf to locate the last
     * occurrence of the period (.) in the file name. Then
     * substring uses the return value of lastIndexOf to
     * extract the file name extension — that is, the
     * substring from the period to the end of the string.
     * This code assumes that the file name has a period in
     * it; if the file name does not have a period,
     * lastIndexOf returns -1, and the substring method
     * throws a StringIndexOutOfBoundsException.
     * 
     * Also, notice that the extension method uses dot + 1
     * as the argument to substring. If the period character
     * (.) is the last character of the string, dot + 1 is
     * equal to the length of the string, which is one
     * larger than the largest index into the string
     * (because indices start at 0). This is a legal
     * argument to substring because that method accepts an
     * index equal to, but not greater than, the length of
     * the string and interprets it to mean
     * "the end of the string."
     */
    public String extension()
    {
        int dot = fullPath.lastIndexOf(extensionSeparator);
        return fullPath.substring(dot + 1);
    }

    // gets filename without extension
    public String filename()
    {
        int dot = fullPath.lastIndexOf(extensionSeparator);
        int sep = fullPath.lastIndexOf(pathSeparator);
        return fullPath.substring(sep + 1, dot);
    }

    public String path()
    {
        int sep = fullPath.lastIndexOf(pathSeparator);
        return fullPath.substring(0, sep);
    }
}
FilenameDemo.java
public class FilenameDemo
{
    public static void main(String[] args)
    {
        final String FPATH = "/home/yahoo/Account.html";
        Filename myHomePage = new Filename(FPATH, '/', '.');
        System.out.println("Extension = " + myHomePage.extension());
        System.out.println("Filename = " + myHomePage.filename());
        System.out.println("Path = " + myHomePage.path());
    }
}
Output
Extension = html
Filename = Account
Path = /home/yahoo
Click the below link to download the code:
https://sites.google.com/site/javaee4321/java/StringDemo_filename_split_App.zip?attredirects=0&d=1

Github Link:
https://github.com/ramram43210/Java/tree/master/BasicJava/StringDemo_filename_split_App

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/627e98cff1455d9113a0f59150e05649fae86225/BasicJava/StringDemo_filename_split_App/?at=master

See also:
  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • No comments:

    Post a Comment