一、前言
关于alibaba的dubbo源码包com.alibaba.dubbo.common.utils.StringUtils字符串工具类,对字符串/字符及其集合数组进行类型判断isInteger/isJavaIdentifier/isNumeric、是否相等isEquals、判空isEmpty、包含contain、分割split、联合join等常见处理,详情参见源码说明。
二、源码说明package com.alibaba.dubbo.common.utils;@b@@b@import com.alibaba.dubbo.common.Constants;@b@import com.alibaba.dubbo.common.io.UnsafeStringWriter;@b@import com.alibaba.fastjson.JSON;@b@import com.alibaba.dubbo.common.logger.Logger;@b@import com.alibaba.dubbo.common.logger.LoggerFactory;@b@@b@import java.io.PrintWriter;@b@import java.util.ArrayList;@b@import java.util.Collection;@b@import java.util.HashMap;@b@import java.util.List;@b@import java.util.Map;@b@import java.util.TreeMap;@b@import java.util.regex.Matcher;@b@import java.util.regex.Pattern;@b@@b@/**@b@ * StringUtils@b@ */@b@@b@public final class StringUtils {@b@@b@ public static final String[] EMPTY_STRING_ARRAY = new String[0];@b@ private static final Logger logger = LoggerFactory.getLogger(StringUtils.class);@b@ private static final Pattern KVP_PATTERN = Pattern.compile("([_.a-zA-Z0-9][-_.a-zA-Z0-9]*)[=](.*)"); //key value pair pattern.@b@@b@ private static final Pattern INT_PATTERN = Pattern.compile("^\\d+$");@b@@b@ private StringUtils() {@b@ }@b@@b@ public static boolean isBlank(String str) {@b@ if (str == null || str.length() == 0)@b@ return true;@b@ return false;@b@ }@b@@b@ /**@b@ * is empty string.@b@ *@b@ * @param str source string.@b@ * @return is empty.@b@ */@b@ public static boolean isEmpty(String str) {@b@ if (str == null || str.length() == 0)@b@ return true;@b@ return false;@b@ }@b@@b@ /**@b@ * is not empty string.@b@ *@b@ * @param str source string.@b@ * @return is not empty.@b@ */@b@ public static boolean isNotEmpty(String str) {@b@ return str != null && str.length() > 0;@b@ }@b@@b@ /**@b@ * @param s1@b@ * @param s2@b@ * @return equals@b@ */@b@ public static boolean isEquals(String s1, String s2) {@b@ if (s1 == null && s2 == null)@b@ return true;@b@ if (s1 == null || s2 == null)@b@ return false;@b@ return s1.equals(s2);@b@ }@b@@b@ /**@b@ * is integer string.@b@ *@b@ * @param str@b@ * @return is integer@b@ */@b@ public static boolean isInteger(String str) {@b@ if (str == null || str.length() == 0)@b@ return false;@b@ return INT_PATTERN.matcher(str).matches();@b@ }@b@@b@ public static int parseInteger(String str) {@b@ if (!isInteger(str))@b@ return 0;@b@ return Integer.parseInt(str);@b@ }@b@@b@ /**@b@ * Returns true if s is a legal Java identifier.
@b@ * more info.@b@ */@b@ public static boolean isJavaIdentifier(String s) {@b@ if (s.length() == 0 || !Character.isJavaIdentifierStart(s.charAt(0))) {@b@ return false;@b@ }@b@ for (int i = 1; i 0 && values != null && values.length > 0) {@b@ for (String v : values) {@b@ if (value.equals(v)) {@b@ return true;@b@ }@b@ }@b@ }@b@ return false;@b@ }@b@@b@ public static boolean isNumeric(String str) {@b@ if (str == null) {@b@ return false;@b@ }@b@ int sz = str.length();@b@ for (int i = 0; i list = null;@b@ char c;@b@ int ix = 0, len = str.length();@b@ for (int i = 0; i ();@b@ list.add(str.substring(ix, i));@b@ ix = i + 1;@b@ }@b@ }@b@ if (ix > 0)@b@ list.add(str.substring(ix));@b@ return list == null ? EMPTY_STRING_ARRAY : (String[]) list.toArray(EMPTY_STRING_ARRAY);@b@ }@b@@b@ /**@b@ * join string.@b@ *@b@ * @param array String array.@b@ * @return String.@b@ */@b@ public static String join(String[] array) {@b@ if (array.length == 0) return "";@b@ StringBuilder sb = new StringBuilder();@b@ for (String s : array)@b@ sb.append(s);@b@ return sb.toString();@b@ }@b@@b@ /**@b@ * join string like javascript.@b@ *@b@ * @param array String array.@b@ * @param split split@b@ * @return String.@b@ */@b@ public static String join(String[] array, char split) {@b@ if (array.length == 0) return "";@b@ StringBuilder sb = new StringBuilder();@b@ for (int i = 0; i 0)@b@ sb.append(split);@b@ sb.append(array[i]);@b@ }@b@ return sb.toString();@b@ }@b@@b@ /**@b@ * join string like javascript.@b@ *@b@ * @param array String array.@b@ * @param split split@b@ * @return String.@b@ */@b@ public static String join(String[] array, String split) {@b@ if (array.length == 0) return "";@b@ StringBuilder sb = new StringBuilder();@b@ for (int i = 0; i 0)@b@ sb.append(split);@b@ sb.append(array[i]);@b@ }@b@ return sb.toString();@b@ }@b@@b@ public static String join(Collection coll, String split) {@b@ if (coll.isEmpty()) return "";@b@@b@ StringBuilder sb = new StringBuilder();@b@ boolean isFirst = true;@b@ for (String s : coll) {@b@ if (isFirst) isFirst = false;@b@ else sb.append(split);@b@ sb.append(s);@b@ }@b@ return sb.toString();@b@ }@b@@b@ /**@b@ * parse key-value pair.@b@ *@b@ * @param str string.@b@ * @param itemSeparator item separator.@b@ * @return key-value map;@b@ */@b@ private static Map parseKeyValuePair(String str, String itemSeparator) {@b@ String[] tmp = str.split(itemSeparator);@b@ Map map = new HashMap(tmp.length);@b@ for (int i = 0; i map = StringUtils.parseQueryString(qs);@b@ return map.get(key);@b@ }@b@@b@ /**@b@ * parse query string to Parameters.@b@ *@b@ * @param qs query string.@b@ * @return Parameters instance.@b@ */@b@ public static Map parseQueryString(String qs) {@b@ if (qs == null || qs.length() == 0)@b@ return new HashMap();@b@ return parseKeyValuePair(qs, "\\&");@b@ }@b@@b@ public static String getServiceKey(Map ps) {@b@ StringBuilder buf = new StringBuilder();@b@ String group = ps.get(Constants.GROUP_KEY);@b@ if (group != null && group.length() > 0) {@b@ buf.append(group).append("/");@b@ }@b@ buf.append(ps.get(Constants.INTERFACE_KEY));@b@ String version = ps.get(Constants.VERSION_KEY);@b@ if (version != null && version.length() > 0) {@b@ buf.append(":").append(version);@b@ }@b@ return buf.toString();@b@ }@b@@b@ public static String toQueryString(Map ps) {@b@ StringBuilder buf = new StringBuilder();@b@ if (ps != null && ps.size() > 0) {@b@ for (Map.Entry entry : new TreeMap(ps).entrySet()) {@b@ String key = entry.getKey();@b@ String value = entry.getValue();@b@ if (key != null && key.length() > 0@b@ && value != null && value.length() > 0) {@b@ if (buf.length() > 0) {@b@ buf.append("&");@b@ }@b@ buf.append(key);@b@ buf.append("=");@b@ buf.append(value);@b@ }@b@ }@b@ }@b@ return buf.toString();@b@ }@b@@b@ public static String camelToSplitName(String camelName, String split) {@b@ if (camelName == null || camelName.length() == 0) {@b@ return camelName;@b@ }@b@ StringBuilder buf = null;@b@ for (int i = 0; i = 'A' && ch <= 'Z') {@b@ if (buf == null) {@b@ buf = new StringBuilder();@b@ if (i > 0) {@b@ buf.append(camelName.substring(0, i));@b@ }@b@ }@b@ if (i > 0) {@b@ buf.append(split);@b@ }@b@ buf.append(Character.toLowerCase(ch));@b@ } else if (buf != null) {@b@ buf.append(ch);@b@ }@b@ }@b@ return buf == null ? camelName : buf.toString();@b@ }@b@@b@ public static String toArgumentString(Object[] args) {@b@ StringBuilder buf = new StringBuilder();@b@ for (Object arg : args) {@b@ if (buf.length() > 0) {@b@ buf.append(Constants.COMMA_SEPARATOR);@b@ }@b@ if (arg == null || ReflectUtils.isPrimitives(arg.getClass())) {@b@ buf.append(arg);@b@ } else {@b@ try {@b@ buf.append(JSON.toJSONString(arg));@b@ } catch (Exception e) {@b@ logger.warn(e.getMessage(), e);@b@ buf.append(arg);@b@ }@b@ }@b@ }@b@ return buf.toString();@b@ }@b@}