Returns the list of non empty path names in the given name Path. - Android java.util

Android examples for java.util:List

Description

Returns the list of non empty path names in the given name Path.

Demo Code

/*// w  w w  .  java 2s.c  o m
 * Copyright (C) 2008-2012 Open Wide SA
 *  
 * This library is free software; you can redistribute 
 * it and/or modify it under the terms of version 2.1 of 
 * the GNU Lesser General Public License as published by  
 * the Free Software Foundation.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General 
 * Public License along with this library; if not, write to the 
 * Free Software Foundation, Inc., 59 Temple Place, Suite 330, 
 * Boston, MA  02111-1307  USA
 * 
 * More information at http://knowledge.openwide.fr/bin/view/Main/AlfrescoETLConnector/
 */
//package com.java2s;
import java.util.ArrayList;

import java.util.StringTokenizer;

public class Main {
    public static void main(String[] argv) {
        String namePath = "java2s.com";
        String pathDelimiter = ".";
        System.out.println(stringToNamePath(namePath, pathDelimiter));
    }

    /**
     * Returns the list of non empty path names in the given namePath.
     * Better than split() (may return "" in the middle) and Spring StringUtils
     * (may return "" anywhere)
     * @param targetLocation
     * @param pathDelimiter
     * @return
     */
    public final static ArrayList<String> stringToNamePath(String namePath,
            String pathDelimiter) {
        StringTokenizer stok = new StringTokenizer(namePath, pathDelimiter);
        ArrayList<String> pathNames = new ArrayList<String>();
        while (stok.hasMoreTokens()) {
            String pathName = stok.nextToken();
            if (pathName.length() == 0) {
                // happens when //
                continue;
            }
            pathNames.add(pathName);
        }
        return pathNames;
    }
}

Related Tutorials