StringUtils.tokenizeToStringArray() 雨点打透心脏的1/2处 2022-11-09 14:09 126阅读 0赞 读Spring源码的时候,遇到这么个东西,记录一下 **BeanDefinitionParserDelegate** /** * Parses the supplied {@code <bean>} element. May return {@code null} * if there were errors during parse. Errors are reported to the * {@link org.springframework.beans.factory.parsing.ProblemReporter}. */ @Nullable public BeanDefinitionHolder parseBeanDefinitionElement(Element ele, @Nullable BeanDefinition containingBean) { // 解析ID属性 String id = ele.getAttribute(ID_ATTRIBUTE); // 解析Name属性 String nameAttr = ele.getAttribute(NAME_ATTRIBUTE); List<String> aliases = new ArrayList<>(); if (StringUtils.hasLength(nameAttr)) { String[] nameArr = StringUtils.tokenizeToStringArray(nameAttr, MULTI_VALUE_ATTRIBUTE_DELIMITERS); aliases.addAll(Arrays.asList(nameArr)); } } 将字符串按照指定的字符转换成String\[\]数组,如字符串中不包含指定字符,则将整个字符串放进数组。如指定字符有多个,是分别按单个字符来切割的。 字符串: “made,in;china” 指定字符: “,;” 返回数组:\[made, in, china\] /** * 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); } /** * Tokenize the given {@code String} into a {@code String} array via a * {@link StringTokenizer}. * <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) * @param trimTokens trim the tokens via {@link String#trim()} * @param ignoreEmptyTokens omit empty tokens from the result array * (only applies to tokens that are empty after trimming; StringTokenizer * will not consider subsequent delimiters as token in the first place). * @return an array of the tokens * @see java.util.StringTokenizer * @see String#trim() * @see #delimitedListToStringArray */ public static String[] tokenizeToStringArray( @Nullable String str, String delimiters, boolean trimTokens, boolean ignoreEmptyTokens) { if (str == null) { return new String[0]; } StringTokenizer st = new StringTokenizer(str, delimiters); List<String> tokens = new ArrayList<>(); while (st.hasMoreTokens()) { String token = st.nextToken(); if (trimTokens) { token = token.trim(); } if (!ignoreEmptyTokens || token.length() > 0) { tokens.add(token); } } return toStringArray(tokens); } 测试代码: public class MyTest { public static final String MULTI_VALUE_ATTRIBUTE_DELIMITERS = ",; "; @Test public void test() { String nameAttr = "made,in;china"; String flag = MULTI_VALUE_ATTRIBUTE_DELIMITERS; String[] nameArr = StringUtils.tokenizeToStringArray(nameAttr, MULTI_VALUE_ATTRIBUTE_DELIMITERS); System.out.println(Arrays.toString(nameArr)); } @Test public void test2() { String nameAttr = "madeinchina"; String flag = MULTI_VALUE_ATTRIBUTE_DELIMITERS; String[] nameArr = StringUtils.tokenizeToStringArray(nameAttr, MULTI_VALUE_ATTRIBUTE_DELIMITERS); System.out.println(Arrays.toString(nameArr)); } } > test输出结果:\[made, in, china\] > test2输出结果:\[madeinchina\]
还没有评论,来说两句吧...