Java String Tokenize tokenizeString(final String inputString, final String seperator)

Here you can find the source of tokenizeString(final String inputString, final String seperator)

Description

Break apart a string using a tokenizer into a List of String s.

License

Open Source License

Parameter

Parameter Description
inputString a string containing zero or or more segments
seperator seperator to use for the split, or null for the default

Return

a List of String s. An emtpy list is returned if inputString is null.

Declaration

public static List<String> tokenizeString(final String inputString, final String seperator) 

Method Source Code


//package com.java2s;
/*//from  www.  j a  v  a  2 s  .  c o m
 * LDAP Chai API
 * Copyright (c) 2006-2010 Novell, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * 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
 */

import java.util.*;

public class Main {
    /**
     * Break apart a string using a tokenizer into a {@code List} of {@code String}s.
     *
     * @param inputString a string containing zero or or more segments
     * @param seperator   seperator to use for the split, or null for the default
     * @return a {@code List} of {@code String}s.  An emtpy list is returned if <i>inputString</i> is null.
     */
    public static List<String> tokenizeString(final String inputString, final String seperator) {
        if (inputString == null || inputString.length() < 1) {
            return Collections.emptyList();
        }

        final List<String> values = new ArrayList<String>();
        values.addAll(Arrays.asList(inputString.split(seperator)));
        return Collections.unmodifiableList(values);
    }

    public static Map<String, String> tokenizeString(final String inputString, final String seperator,
            final String subSeperator) {
        if (inputString == null || inputString.length() < 1) {
            return new HashMap<String, String>();
        }

        final Map<String, String> returnProps = new LinkedHashMap<String, String>();

        final List<String> values = tokenizeString(inputString, seperator);
        for (final String loopValue : values) {
            if (loopValue != null && loopValue.length() > 0) {
                final int subSeperatorPosition = loopValue.indexOf(subSeperator);
                if (subSeperatorPosition != -1) {
                    final String key = loopValue.substring(0, subSeperatorPosition);
                    final String value = loopValue.substring(subSeperatorPosition + 1);
                    returnProps.put(key, value);
                } else {
                    returnProps.put(loopValue, "");
                }
            }
        }
        return returnProps;
    }
}

Related

  1. tokenizePathAsArray(String path)
  2. tokenizePattern(String pattern)
  3. tokenizeQuotedStrings(final String aInput, final String aDelimiters)
  4. tokenizeQuotes(String f1)
  5. tokenizeStatement(String statement)
  6. tokenizeString(String inString, char delimiter, String enclosures)
  7. tokenizeStringArray(String[] array, String token)
  8. tokenizeStringWithQuotes(String line, String quoteStyle)
  9. tokenizeToStringArray(String str, String delimiters)