Example usage for java.util StringTokenizer nextToken

List of usage examples for java.util StringTokenizer nextToken

Introduction

In this page you can find the example usage for java.util StringTokenizer nextToken.

Prototype

public String nextToken() 

Source Link

Document

Returns the next token from this string tokenizer.

Usage

From source file:StringUtil.java

/**
 * Parses a list according to the specified delimiter into an
 * array of Strings.//from   w ww  .  ja  v  a  2  s  .  com
 * @see java.util.StringTokenizer
 * @param list a string of token seperated values
 * @param delim the delimiter character(s).  Each character in the string is a
 * single delimeter.
 * @return an array of strings
 */
public static String[] parseList(String list, String delim) {
    List result = new ArrayList();
    StringTokenizer tokenizer = new StringTokenizer(list, delim);
    while (tokenizer.hasMoreTokens()) {
        result.add(tokenizer.nextToken());
    }
    return (String[]) result.toArray(new String[0]);
}

From source file:Main.java

public static String[] parseExtensions(String extensions) {
    StringTokenizer st = new StringTokenizer(extensions, ",");
    String[] ext = new String[st.countTokens()];
    int i = 0;//from   w  ww  .ja v a 2  s . c  o  m
    while (st.hasMoreTokens()) {
        ext[i++] = st.nextToken();
    }
    return ext;
}

From source file:Main.java

public static String parseValue(Map<String, List<String>> responseHeader, String parentKey, String valueKey) {
    List<String> contentTypes = responseHeader.get(parentKey);
    if (contentTypes != null && contentTypes.size() > 0) {
        String contentType = contentTypes.get(0);
        StringTokenizer stringTokenizer = new StringTokenizer(contentType, ";");
        while (stringTokenizer.hasMoreTokens()) {
            String token = stringTokenizer.nextToken();
            if (token.contains(valueKey)) {
                String[] values = token.split("=");
                if (values.length > 1) {
                    return values[1];
                }//from   w  w w  .j  a  v  a  2  s .  c  o  m
            }
        }
    }
    return null;
}

From source file:Main.java

public static String quote(String value) {
    if (value != null) {
        if (value.contains("'")) {
            if (value.contains("\"")) {
                // We've got something perverse like [["It's back!"]] (ie a mix of single and double quotes!)
                StringBuilder sb = new StringBuilder("concat(");

                StringTokenizer st = new StringTokenizer(value, "'\"", true);
                while (st.hasMoreTokens()) {
                    sb.append(quote(st.nextToken()));

                    if (st.hasMoreTokens()) {
                        sb.append(", ");
                    }//  w  w  w  .j  av  a  2 s.c  o m
                }

                sb.append(")");

                return sb.toString();
            } else {
                return '"' + value + '"';
            }

        } else {
            return "'" + value + "'";
        }
    }

    return value;
}

From source file:Main.java

/**
 * Creates a list of integers from a string within tokens. Empty tokens will
 * be omitted: If a string within tokens is <code>"1,,2,,3"</code> and the
 * delimiter string is <code>","</code>, the returned list of integers
 * contains the tree elements <code>1, 2, 3</code>.
 *
 * <em>It is expected, that each token can be parsed as an integer or is
 * empty!</em>/*from  w  ww .j  a v  a2  s  .c om*/
 *
 * @param string    String within tokens parsable as integer
 * @param delimiter Delimiter between the integer tokens. Every character
 *                  of the delimiter string is a separate delimiter. If
 *                  the string within tokens is <code>"1,2:3"</code>
 *                  and the delimiter string is <code>",:"</code>, the
 *                  returned list of integers contains the three elements
 *                  <code>1, 2, 3</code>.
 * @return          list of integers
 * @throws          NumberFormatException if the string contains a not empty
 *                  token that can't parsed as an integer
 */
public static List<Integer> integerTokenToList(String string, String delimiter) {
    if (string == null) {
        throw new NullPointerException("string == null");
    }

    if (delimiter == null) {
        throw new NullPointerException("delimiter == null");
    }

    List<Integer> integerList = new ArrayList<>();
    StringTokenizer tokenizer = new StringTokenizer(string, delimiter);

    while (tokenizer.hasMoreTokens()) {
        integerList.add(Integer.parseInt(tokenizer.nextToken()));
    }

    return integerList;
}

From source file:Main.java

public static String[] split(String str, String delimiters) {
    final StringTokenizer st = new StringTokenizer(str, delimiters);
    final List<String> tokens = new ArrayList<String>();
    while (st.hasMoreTokens()) {
        final String token = st.nextToken();
        tokens.add(token);/* w  w  w. j  av  a 2  s.c  o  m*/
    }
    return tokens.toArray(new String[tokens.size()]);
}

From source file:Main.java

/**
 * A value of the header information./*w w  w  .j  a va  2s.c  om*/
 *
 * @param content      like {@code text/html;charset=utf-8}.
 * @param key          like {@code charset}.
 * @param defaultValue list {@code utf-8}.
 * @return If you have a value key, you will return the parsed value if you don't return the default value.
 */
public static String parseHeadValue(String content, String key, String defaultValue) {
    if (!TextUtils.isEmpty(content) && !TextUtils.isEmpty(key)) {
        StringTokenizer stringTokenizer = new StringTokenizer(content, ";");
        while (stringTokenizer.hasMoreElements()) {
            String valuePair = stringTokenizer.nextToken();
            int index = valuePair.indexOf('=');
            if (index > 0) {
                String name = valuePair.substring(0, index).trim();
                if (key.equalsIgnoreCase(name)) {
                    defaultValue = valuePair.substring(index + 1).trim();
                    break;
                }
            }
        }
    }
    return defaultValue;
}

From source file:Main.java

public static Vector<String> getSets(String setString) {
    StringTokenizer tokenizer = new StringTokenizer(setString, ";");
    Vector<String> sets = new Vector<String>();
    while (tokenizer.hasMoreTokens()) {
        sets.add(tokenizer.nextToken());
    }//  ww w .  j  a  v  a 2 s.  c om
    if (sets.size() == 0)
        sets.add("");
    return sets;
}

From source file:Main.java

public static void dumpParameters(Parameters parameters) {
    String flattened = parameters.flatten();
    StringTokenizer tokenizer = new StringTokenizer(flattened, ";");
    Log.d(TAG, "Dump all camera parameters:");
    while (tokenizer.hasMoreElements()) {
        Log.d(TAG, tokenizer.nextToken());
    }/* w w  w.  jav  a 2  s  .  co  m*/
}

From source file:com.googlecode.jsfFlex.myFaces.LocaleUtils.java

/**
 * Convert locale string used by converter tags to locale.
 *
 * @param name name of the locale//from  w w w . j a v a2s  .c o  m
 * @return locale specified by the given String
 *
 * @see org.apache.myfaces.taglib.core.ConvertDateTimeTag#setConverterLocale
 * @see org.apache.myfaces.taglib.core.ConvertNumberTag#setConverterLocale
 */
public static Locale converterTagLocaleFromString(String name) {
    try {
        Locale locale;
        StringTokenizer st = new StringTokenizer(name, "_");
        String language = st.nextToken();

        if (st.hasMoreTokens()) {
            String country = st.nextToken();

            if (st.hasMoreTokens()) {
                String variant = st.nextToken();
                locale = new Locale(language, country, variant);
            } else {
                locale = new Locale(language, country);
            }
        } else {
            locale = new Locale(language);
        }

        return locale;
    } catch (Exception e) {
        throw new IllegalArgumentException(
                "Locale parsing exception - " + "invalid string representation '" + name + "'");
    }
}