Java Set sub set getFirstSubString(String s, Set delimiters)

Here you can find the source of getFirstSubString(String s, Set delimiters)

Description

get First Sub String

License

Open Source License

Parameter

Parameter Description
s the input string

Return

a substring of s (or null, if the input was null), ending at the first appearance of one of the given delimiters (or at the first whitespace if no delimiters were given)

Declaration

public static String getFirstSubString(String s, Set<String> delimiters) 

Method Source Code

//package com.java2s;
//it under the terms of the GNU Affero General Public License as published by

import java.util.Iterator;

import java.util.Set;

public class Main {
    /**/* ww w . j  a v a  2 s.  c o  m*/
     * @return a substring of s (or null, if the input was null),
     *  ending at the first appearance of one of the given delimiters (or at the first whitespace if no delimiters were given)
     * @param s the input string
     */
    public static String getFirstSubString(String s, Set<String> delimiters) {
        if (s == null)
            return null;

        String sub = s.trim();
        int endIndex = -1;

        if (delimiters != null) {
            Iterator<String> iter = delimiters.iterator();

            while (iter.hasNext()) {
                String delimiter = iter.next();
                if (delimiter != null && (endIndex < 0 || sub.indexOf(delimiter) < endIndex))
                    endIndex = sub.indexOf(delimiter);
            }
        }

        if (endIndex < 0)
            endIndex = sub.indexOf(" ");

        return endIndex < 0 ? s : sub.substring(0, endIndex);
    }

    /**
     * @param s
     * @return a trimmed version of the given String - leading and trailing whitespace are omitted.
     * @postcondition (s == null) --> result == null
     * @postcondition (s != null) --> result.equals(s.trim())
     * @see String#trim()
     */
    public static String trim(String s) {
        return (s == null) ? null : s.trim();
    }
}

Related

  1. getSubsets(Set set)
  2. split(Set original, int subsetSize)
  3. sub(Set a, Set b)
  4. subset(double[] vals, boolean[] select)