自定义工具类之“分割所有类型的字符串”

自定义工具类之“分割所有类型的字符串”

    /**
     * <p>默认根据,, \t\n\r分隔符分隔出list</p>
     *
     * <pre>
     * tokenizeToStringArray(null)            = null
     * tokenizeToStringArray("www.baidu.com") = [www.baidu.com]
     * tokenizeToStringArray("a,b,c")         = [a, b, b]
     * </pre>
     * @param str  the String to check, may be null
     * @return an list of parsed Strings, null if null String input
     */
    public static List<String> tokenizeToStringList(String str)
    {
        return tokenizeToStringList(str, ",, \t\n\r");
    }

 

被调用方法

    /**
     * <p>根据分隔符分隔出list</p>
     *
     * <pre>
     * tokenizeToStringArray(null, " \t\n\r\f")     = null
     * tokenizeToStringArray("www.baidu.com", ".b") = [www, aidu, com]
     * tokenizeToStringArray("a,b,c", ",, \t\n\r")  = [a, b, b]
     * </pre>
     * @param str  the String to check, may be null
     * @param delimiters the delimiter characters, assembled as a {@code String}
     * @return an list of parsed Strings, null if null String input
     */
    public static List<String> tokenizeToStringList(String str, String delimiters)
    {
        if (str == null)
        {
            return null;
        }

        StringTokenizer st = new StringTokenizer(str, delimiters);
        List<String> tokens = new ArrayList<String>();
        while (st.hasMoreTokens())
        {
            String token = st.nextToken().trim();
            if (token.length() > 0)
            {
                tokens.add(token);
            }
        }

        return tokens;
    }

 

posted @ 2022-12-31 11:06  骚哥  阅读(60)  评论(0编辑  收藏  举报