Example usage for java.util StringTokenizer StringTokenizer

List of usage examples for java.util StringTokenizer StringTokenizer

Introduction

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

Prototype

public StringTokenizer(String str, String delim) 

Source Link

Document

Constructs a string tokenizer for the specified string.

Usage

From source file:TokenizerUtil.java

public static String[] convertCSVStringToArray(String s) {
    ArrayList<String> list = new ArrayList<String>();
    StringTokenizer st = new StringTokenizer(s, "|");
    while (st.hasMoreTokens()) {
        String token = st.nextToken();
        list.add(token);/*  w ww. j  a  va2 s  . co  m*/
    }
    return (String[]) list.toArray(new String[list.size()]);
}

From source file:Main.java

/**
 * get the  text string in an element (eg interspersed between child elements), 
 * or "" if there is none or if the Element is null.
 * Tries to ignore white space text; but does not succeed.
 *//*from  www. ja va  2s .c  o m*/
public static String getText(Element el) {
    String res = "";
    if (el != null)
        try {
            el.normalize(); // does not help recognise white space
            NodeList nodes = el.getChildNodes();
            for (int i = 0; i < nodes.getLength(); i++)
                if (nodes.item(i) instanceof Text) {
                    Text text = (Text) nodes.item(i);
                    // this filter seems to make no difference
                    if (!text.isElementContentWhitespace()) {
                        String tData = text.getData();
                        // this seems to be an effective way to catch pure white space
                        StringTokenizer nonWhiteSpace = new StringTokenizer(tData, "\n \t");
                        if (nonWhiteSpace.countTokens() > 0)
                            res = res + tData;
                    }
                }
        } catch (Exception e) {
            System.out.println("Text failure: " + e.getMessage());
        }
    return res;
}

From source file:Main.java

/**
 * utility method to get the last token in a "."-delimited package+classname string
 *
 * @return/*  w ww . j a  v a2  s.co m*/
 */
private static String getSimpleName(String in) {
    if (in == null || in.length() == 0) {
        return in;
    }
    String out = null;
    StringTokenizer tokenizer = new StringTokenizer(in, ".");
    if (tokenizer.countTokens() == 0)
        out = in;
    else {
        while (tokenizer.hasMoreTokens()) {
            out = tokenizer.nextToken();
        }
    }
    return out;
}

From source file:Main.java

/**
 * Parses a string into a locale. The string is expected to be of the same
 * format of the string obtained by calling Locale.toString().
 * /*from   w w  w.  j a  v a  2s.com*/
 * @param localeString
 *            string representation of a locale
 * @return a locale or <code>null</code> if string is empty or null
 */
public static Locale parseLocale(String localeString) {
    if (localeString == null || localeString.trim().length() == 0) {
        return null;
    }
    StringTokenizer tokens = new StringTokenizer(localeString, "_"); //$NON-NLS-1$
    List<String> localeSections = new ArrayList<String>();
    while (tokens.hasMoreTokens()) {
        localeSections.add(tokens.nextToken());
    }
    Locale locale = null;
    switch (localeSections.size()) {
    case 1:
        locale = new Locale(localeSections.get(0));
        break;
    case 2:
        locale = new Locale(localeSections.get(0), localeSections.get(1));
        break;
    case 3:
        locale = new Locale(localeSections.get(0), localeSections.get(1), localeSections.get(2));
        break;
    default:
        break;
    }
    return locale;
}

From source file:Main.java

public static final String[] toStringArray(String text, String token) {
    if (text == null || text.length() == 0) {
        return new String[0];
    }/*from   w w w .  j a  v  a  2s  .  co m*/
    StringTokenizer tokens = new StringTokenizer(text, token);
    String[] words = new String[tokens.countTokens()];
    for (int i = 0; i < words.length; i++) {
        words[i] = tokens.nextToken();
    }
    return words;
}

From source file:Main.java

/**
 * Searches for a node within a DOM document with a given node path expression.
 * Elements are separated by '.' characters.
 * Example: Foo.Bar.Poo/*  w  w  w . j a  va2s  .c om*/
 * @param doc DOM Document to search for a node.
 * @param pathExpression dot separated path expression
 * @return Node element found in the DOM document.
 */
public static Node findNodeByName(Document doc, String pathExpression) {
    final StringTokenizer tok = new StringTokenizer(pathExpression, ".");
    final int numToks = tok.countTokens();
    NodeList elements;
    if (numToks == 1) {
        elements = doc.getElementsByTagNameNS("*", pathExpression);
        return elements.item(0);
    }

    String element = pathExpression.substring(pathExpression.lastIndexOf('.') + 1);
    elements = doc.getElementsByTagNameNS("*", element);

    String attributeName = null;
    if (elements.getLength() == 0) {
        //No element found, but maybe we are searching for an attribute
        attributeName = element;

        //cut off attributeName and set element to next token and continue
        Node found = findNodeByName(doc,
                pathExpression.substring(0, pathExpression.length() - attributeName.length() - 1));

        if (found != null) {
            return found.getAttributes().getNamedItem(attributeName);
        } else {
            return null;
        }
    }

    StringBuffer pathName;
    Node parent;
    for (int j = 0; j < elements.getLength(); j++) {
        int cnt = numToks - 1;
        pathName = new StringBuffer(element);
        parent = elements.item(j).getParentNode();
        do {
            if (parent != null) {
                pathName.insert(0, '.');
                pathName.insert(0, parent.getLocalName());//getNodeName());

                parent = parent.getParentNode();
            }
        } while (parent != null && --cnt > 0);
        if (pathName.toString().equals(pathExpression)) {
            return elements.item(j);
        }
    }

    return null;
}

From source file:Util.java

/**
 * Split "str" into tokens by delimiters and optionally remove white spaces
 * from the splitted tokens.//from   ww  w.  j  a va  2s .  com
 *
 * @param trimTokens if true, then trim the tokens
 */
public static String[] split(String str, String delims, boolean trimTokens) {
    StringTokenizer tokenizer = new StringTokenizer(str, delims);
    int n = tokenizer.countTokens();
    String[] list = new String[n];
    for (int i = 0; i < n; i++) {
        if (trimTokens) {
            list[i] = tokenizer.nextToken().trim();
        } else {
            list[i] = tokenizer.nextToken();
        }
    }
    return list;
}

From source file:Main.java

public static String encodeUrl(String url, String charset) throws UnsupportedEncodingException {
    if (url == null) {
        return "";
    }//w w w . j  a va2s  . com

    int index = url.indexOf("?");
    if (index >= 0) {

        String result = url.substring(0, index + 1);
        String paramsPart = url.substring(index + 1);
        StringTokenizer tokenizer = new StringTokenizer(paramsPart, "&");
        while (tokenizer.hasMoreTokens()) {
            String definition = tokenizer.nextToken();
            int eqIndex = definition.indexOf("=");
            if (eqIndex >= 0) {
                String paramName = definition.substring(0, eqIndex);
                String paramValue = definition.substring(eqIndex + 1);
                result += paramName + "=" + encodeUrlParam(paramValue, charset) + "&";
            } else {
                result += encodeUrlParam(definition, charset) + "&";
            }
        }

        if (result.endsWith("&")) {
            result = result.substring(0, result.length() - 1);
        }

        return result;

    }

    return url;
}

From source file:Main.java

/**
 * /* w  ww .  j  a v a  2  s. c  o m*/
 * @param value
 * @return int[]
 */
public static int[] toIntArray(final String value) {
    if (value == null) {
        return new int[] {};
    }

    final String strippedValue = value.replace(ARRAY_OPEN_TAG, EMPTY_STRING).replace(ARRAY_CLOSE_TAG,
            EMPTY_STRING);
    final StringTokenizer tokenizer = new StringTokenizer(strippedValue, ELEMENT_SEPARATOR);
    final Collection<Integer> intCollection = new ArrayList<>();

    while (tokenizer.hasMoreTokens()) {
        intCollection.add(Integer.valueOf(tokenizer.nextToken().trim()));
    }

    return toIntArray(intCollection);
}

From source file:Main.java

/**
 * A value of the header information.//w w w  .j  a  v a  2  s .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;
}