zoukankan      html  css  js  c++  java
  • 记录Java8中的字符串转数组再通过指定符号拼接

    1.字符串转换字符串数组

    String[] nameArr = StringUtils.tokenizeToStringArray(nameAttr, MULTI_VALUE_ATTRIBUTE_DELIMITERS);  // nameAttr="made,in;china,;qqq,.aaa" ,MULTI_VALUE_ATTRIBUTE_DELIMITERS=",;."

    将字符串按照指定的字符转换成String[]数组,如字符串中不包含指定字符,则将整个字符串放进数组。如指定字符有多个,是分别按所有可能出现的字符来切割的。 字符串: "made,in;china,;qqq,.aaa"     指定字符: ",;."       返回数组:[made, in, china, qqq, aaa]

    /**
     * Tokenize the given {@code String} into a {@code String} array via a
     * {@link StringTokenizer}.
     * <p>Trims tokens and omits empty tokens.
     * <p>The given {@code delimiters} string can consist of any number of
     * delimiter characters. Each of those characters can be used to separate
     * tokens. A delimiter is always a single character; for multi-character
     * delimiters, consider using {@link #delimitedListToStringArray}.
     * @param str the {@code String} to tokenize (potentially {@code null} or empty)
     * @param delimiters the delimiter characters, assembled as a {@code String}
     * (each of the characters is individually considered as a delimiter)
     * @return an array of the tokens
     * @see java.util.StringTokenizer
     * @see String#trim()
     * @see #delimitedListToStringArray
     */
    public static String[] tokenizeToStringArray(@Nullable String str, String delimiters) {
        return tokenizeToStringArray(str, delimiters, true, true);
    }

     2.字符串数组通过某符号连接

      Collectors.joining 收集器 支持灵活的参数配置,可以指定字符串连接时的 分隔符,前缀 和 后缀 字符串

    Stream<String> stream = Arrays.stream(nameArr);
    String result = stream.collect(Collectors.joining("/", "[", "]"));
    System.out.println(result);
    // 输出字符串 [made/in/china]
  • 相关阅读:
    类的组合
    类的继承和派生
    面向对象编程
    正则表达式
    sys模块 logging模块 序列化模块
    time 模块,random模块,os模块
    递归函数
    interface有没有继承Object
    UTF-8和GBK的区别
    九皇后
  • 原文地址:https://www.cnblogs.com/qlnx/p/14470201.html
Copyright © 2011-2022 走看看