Java Vector from String stringVector(String input, String sepChars)

Here you can find the source of stringVector(String input, String sepChars)

Description

This method takes a sepChars-separated string and converts it to a vector of fields.

License

Open Source License

Parameter

Parameter Description
input the sepChars-separated string to test.
sepChars a string containing a list of characters which may occur as field separators. Any two fields in the input may be separated by one or many of the characters present in sepChars.

Declaration


public static Vector stringVector(String input, String sepChars) 

Method Source Code


//package com.java2s;
/*/*ww  w .j a  va2s  .  c o m*/
    
  VectorUtils.java
    
  Convenience methods for working with Vectors.. provides efficient Union,
  Intersection, and Difference methods.
      
  Created: 21 July 1998
  Release: $Name:  $
  Version: $Revision: 1.21 $
  Last Mod Date: $Date: 2002/11/01 02:25:10 $
  Module By: Jonathan Abbey, jonabbey@arlut.utexas.edu
    
  -----------------------------------------------------------------------
          
  Ganymede Directory Management System
    
  Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002
  The University of Texas at Austin.
    
  Contact information
    
  Web site: http://www.arlut.utexas.edu/gash2
  Author Email: ganymede_author@arlut.utexas.edu
  Email mailing list: ganymede@arlut.utexas.edu
    
  US Mail:
    
  Computer Science Division
  Applied Research Laboratories
  The University of Texas at Austin
  PO Box 8029, Austin TX 78713-8029
    
  Telephone: (512) 835-3200
    
  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.
    
  This program 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 General Public License for more details.
    
  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
    
*/

import java.util.*;

public class Main {
    /**
     * <P>This method takes a sepChars-separated string and converts it to
     * a vector of fields.  i.e., "gomod,jonabbey" -> a vector whose
     * elements are "gomod" and "jonabbey".</P>
     *
     * <P>NOTE: this method will omit 'degenerate' fields from the output
     * vector.  That is, if input is "gomod,,,  jonabbey" and sepChars
     * is ", ", then the result vector will still only have "gomod"
     * and "jonabbey" as elements, even though one might wish to
     * explicitly know about the blanks between commas.  This method
     * is intended mostly for creating email list vectors, rather than
     * general file-parsing vectors.</P>
     *
     * @param input the sepChars-separated string to test.
     *
     * @param sepChars a string containing a list of characters which
     * may occur as field separators.  Any two fields in the input may
     * be separated by one or many of the characters present in sepChars.
     */

    public static Vector stringVector(String input, String sepChars) {
        Vector results = new Vector();
        int index = 0;
        int oldindex = 0;
        String temp;
        char inputAry[] = input.toCharArray();

        /* -- */

        while (index != -1) {
            // skip any leading field-separator chars

            for (; oldindex < input.length(); oldindex++) {
                if (sepChars.indexOf(inputAry[oldindex]) == -1) {
                    break;
                }
            }

            if (oldindex == input.length()) {
                break;
            }

            index = findNextSep(input, oldindex, sepChars);

            if (index == -1) {
                temp = input.substring(oldindex);

                // System.err.println("+ " + temp + " +");

                results.addElement(temp);
            } else {
                temp = input.substring(oldindex, index);

                // System.err.println("* " + temp + " *");

                results.addElement(temp);

                oldindex = index + 1;
            }
        }

        return results;
    }

    /**
     * <P>findNextSep() takes a string, a starting position, and a string of
     * characters to be considered field separators, and returns the
     * first index after startDex whose char is in sepChars.</P>
     *
     * <P>If there are no chars in sepChars past startdex in input, findNextSep()
     * returns -1.</P>
     */

    private static int findNextSep(String input, int startDex, String sepChars) {
        int currentIndex = input.length();
        char sepAry[] = sepChars.toCharArray();
        boolean foundSep = false;

        /* -- */

        // find the next separator

        for (int i = 0; i < sepAry.length; i++) {
            int tempdex = input.indexOf(sepAry[i], startDex);

            if (tempdex > -1 && tempdex <= currentIndex) {
                currentIndex = tempdex;
                foundSep = true;
            }
        }

        if (foundSep) {
            return currentIndex;
        } else {
            return -1;
        }
    }
}

Related

  1. reverseVector(final Vector src)
  2. reverseVector(Vector rc)
  3. string2Vector(String s, String delimiter)
  4. stringArrayToVector(String[] array)
  5. stringToVector(String[] arr)
  6. unique(Vector list)
  7. vectorContainsIgnoreCaseFirstLetter(Vector v, String value)
  8. vectorString(String splitStr, String delim)