首页

定义字符串处理辅助类StringHelper进行字符串的查找连接分割等处理代码代码示例

标签:字符串处理工具类,StringHelper,字符串查找,base64加密     发布时间:2018-11-29   

一、前言

定义辅助字符串处理类StringHelper,进行字符串指定分割处理、基于数组或序列指定字符串查找、字符串base64加密解密、空判断及大小写处理、字符串分割连接等相关处理,详情代码示例。

二、代码示例

import java.util.ArrayList;@b@import java.util.Iterator;@b@import java.util.List;@b@import org.apache.commons.lang.StringUtils;@b@import sun.misc.BASE64Decoder;@b@import sun.misc.BASE64Encoder;@b@@b@public final class StringHelper {@b@	public static final String ENCODE_TYPE = "UTF-8";@b@@b@	public static String getStart(String mainStr, String splitStr) {@b@		if ((StringUtils.isEmpty(mainStr)) || (StringUtils.isEmpty(splitStr))@b@				|| (mainStr.length() < splitStr.length()))@b@			return null;@b@		String result = mainStr.substring(0, splitStr.length());@b@		if (result.equalsIgnoreCase(splitStr))@b@			return result;@b@		return null;@b@	}@b@@b@	public static String joinStart(String mainStr, String splitStr) {@b@		if (StringUtils.isEmpty(mainStr))@b@			return splitStr;@b@		if (StringUtils.isEmpty(splitStr))@b@			return mainStr;@b@		mainStr = mainStr.trim();@b@		String startStr = getStart(mainStr, splitStr);@b@		return ((startStr == null) ? splitStr + mainStr : mainStr);@b@	}@b@@b@	public static String getServiceId(Class class1) {@b@		String name = class1.getName();@b@		int i = name.lastIndexOf(46);@b@		String firstChar = name.substring(i + 1, i + 2).toLowerCase();@b@		String others = name.substring(i + 2);@b@		return firstChar + others;@b@	}@b@@b@	public static boolean find(String[] strs, String s) {@b@		for (int i = 0; i < strs.length; ++i)@b@			if (strs[i].equals(s))@b@				return true;@b@@b@		return false;@b@	}@b@@b@	public static boolean find(List strs, String s) {@b@		for (Iterator it = strs.iterator(); it.hasNext();) {@b@			String string = (String) it.next();@b@			if (string.equals(s))@b@				return true;@b@		}@b@		return false;@b@	}@b@@b@	public static String base64(String str) {@b@		if (StringUtils.isEmpty(str))@b@			return str;@b@		return new BASE64Encoder().encode(str.getBytes());@b@	}@b@@b@	public static String unbase64(String str) {@b@		if (StringUtils.isEmpty(str))@b@			return str;@b@		BASE64Decoder decoder = new BASE64Decoder();@b@		try {@b@			byte[] b = decoder.decodeBuffer(str);@b@			return new String(b);@b@		} catch (Exception e) {@b@			throw new RuntimeException(e);@b@		}@b@	}@b@@b@	public static String notNull(String str) {@b@		return ((str == null) ? "" : str);@b@	}@b@@b@	public static String checkBlank(String str, String defaultStr) {@b@		return ((StringUtils.isBlank(str)) ? defaultStr : str);@b@	}@b@@b@	public static String notBlank(String str) {@b@		return (((str == null) || (StringUtils.isBlank(str))) ? null : str);@b@	}@b@@b@	public static String notBlankAndTrim(String str) {@b@		return (((str == null) || (StringUtils.isBlank(str))) ? null : str@b@				.trim());@b@	}@b@@b@	public static String lowerCaseFirstChar(String s) {@b@		return s.substring(0, 1).toLowerCase() + s.substring(1);@b@	}@b@@b@	public static String upperCaseFirstChar(String s) {@b@		return s.substring(0, 1).toUpperCase() + s.substring(1);@b@	}@b@@b@	public static String join(List list) {@b@		if (list == null)@b@			return "";@b@@b@		return StringUtils.join(list.iterator(), ",");@b@	}@b@@b@	public static String[] splitAndTrim(String str) {@b@		if (str == null)@b@			return new String[0];@b@		String[] strArray = StringUtils.split(str, ",");@b@		String[] result = new String[strArray.length];@b@		for (int i = 0; i < strArray.length; ++i)@b@			result[i] = strArray[i].trim();@b@@b@		return result;@b@	}@b@@b@	public static List split(String str) {@b@		if (str == null)@b@			return new ArrayList();@b@		String[] strArray = StringUtils.split(str, ",");@b@		List result = new ArrayList(strArray.length);@b@		for (int i = 0; i < strArray.length; ++i)@b@			result.add(strArray[i]);@b@@b@		return result;@b@	}@b@@b@	public static List split(String str, String spaceChar) {@b@		if (str == null)@b@			return new ArrayList();@b@		String[] strArray = StringUtils.split(str, spaceChar);@b@		List result = new ArrayList(strArray.length);@b@		for (int i = 0; i < strArray.length; ++i)@b@			result.add(strArray[i]);@b@@b@		return result;@b@	}@b@@b@	public static String join(byte[] bytes) {@b@		if (bytes == null)@b@			return "";@b@@b@		StringBuffer buf = new StringBuffer(bytes.length * 4);@b@		for (int i = 0; i < bytes.length; ++i)@b@			buf.append(bytes[i]).append(",");@b@@b@		return buf.toString();@b@	}@b@@b@	public static byte[] splitBytes(String str) {@b@		if (str == null)@b@			return new byte[0];@b@@b@		String[] strs = StringUtils.split(str, ",");@b@		byte[] bytes = new byte[strs.length];@b@		for (int i = 0; i < strs.length; ++i)@b@			bytes[i] = Byte.parseByte(strs[i]);@b@@b@		return bytes;@b@	}@b@@b@	public static String getValName(String val) {@b@		if (val == null)@b@			return null;@b@		val = val.trim();@b@		if ((val.startsWith("${")) && (val.endsWith("}")))@b@			return val.substring(2, val.length() - 1).trim();@b@@b@		return null;@b@	}@b@}