import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;/*** 字符串工具类,提供一些字符串相关的便捷方法*/
public class StringUtil {private StringUtil() {throw new AssertionError();}/*** <pre>* isBlank(null) = true;* isBlank("") = true;* isBlank("  ") = true;* isBlank("a") = false;* isBlank("a ") = false;* isBlank(" a") = false;* isBlank("a b") = false;* </pre>** @param str 字符串* @return 如果字符串为空或者长度为0,返回true,否则返回false*/public static boolean isBlank(String str) {return (str == null || str.trim().length() == 0);}/*** <pre>* isEmpty(null) = true;* isEmpty("") = true;* isEmpty("  ") = false;* </pre>** @param c 字符序列* @return 如果字符序列为空或者长度为0,返回true,否则返回false*/public static boolean isEmpty(CharSequence c) {return (c == null || c.length() == 0);}/*** 获取字符序列的长度* <pre>* length(null) = 0;* length(\"\") = 0;* length(\"abc\") = 3;* </pre>** @param c 字符序列* @return 如果字符序列为空,返回0,否则返回字符序列的长度*/public static int length(CharSequence c) {return c == null ? 0 : c.length();}/*** null Object to empty string*   空对象转化成空字符串* <pre>* nullStrToEmpty(null) = "";* nullStrToEmpty("") = "";* nullStrToEmpty("aa") = "aa";* </pre>** @param object 对象* @return String*/public static String nullStrToEmpty(Object object) {return object == null ?"" : (object instanceof String ? (String)object : object.toString());}/*** @param str str* @return String*/public static String capitalizeFirstLetter(String str) {if (isEmpty(str)) {return str;}char c = str.charAt(0);return (!Character.isLetter(c) || Character.isUpperCase(c))? str: new StringBuilder(str.length()).append(Character.toUpperCase(c)).append(str.substring(1)).toString();}/*** 用utf-8编码* @param str 字符串* @return 返回一个utf8的字符串*/public static String utf8Encode(String str) {if (!isEmpty(str) || str.getBytes().length != str.length()) {try {return URLEncoder.encode(str, "utf-8");} catch (UnsupportedEncodingException e) {throw new RuntimeException("UnsupportedEncodingException occurred. ", e);}}return str;}/*** @param href 字符串* @return 返回一个html*/public static String getHrefInnerHtml(String href) {if (isEmpty(href)) {return "";}String hrefReg = ".*<[\\s]*a[\\s]*.*>(.+?)<[\\s]*/a[\\s]*>.*";Pattern hrefPattern = Pattern.compile(hrefReg, Pattern.CASE_INSENSITIVE);Matcher hrefMatcher = hrefPattern.matcher(href);if (hrefMatcher.matches()) {return hrefMatcher.group(1);}return href;}/*** @param source 字符串* @return 返回htmL到字符串*/public static String htmlEscapeCharsToString(String source) {return StringUtil.isEmpty(source)? source: source.replaceAll("<", "<").replaceAll(">", ">").replaceAll("&", "&").replaceAll(""", "\""); }/*** @param s 字符串* @return String*/public static String fullWidthToHalfWidth(String s) {if (isEmpty(s)) {return s;}char[] source = s.toCharArray();for (int i = 0; i < source.length; i++) {if (source[i] == 12288) {source[i] = ' ';// } else if (source[i] == 12290) {// source[i] = '.';}else if (source[i] >= 65281 && source[i] <= 65374) {source[i] = (char) (source[i] - 65248);}else {source[i] = source[i];}}return new String(source);}/*** @param s 字符串* @return 返回的数值*/public static String halfWidthToFullWidth(String s) {if (isEmpty(s)) {return s;}char[] source = s.toCharArray();for (int i = 0; i < source.length; i++) {if (source[i] == ' ') {source[i] = (char) 12288;// } else if (source[i] == '.') {// source[i] = (char)12290;}else if (source[i] >= 33 && source[i] <= 126) {source[i] = (char) (source[i] + 65248);}else {source[i] = source[i];}}return new String(source);}/*** @param str 资源* @return 特殊字符串切换*/public static String replaceBlanktihuan(String str) {String dest = "";if (str != null) {Pattern p = Pattern.compile("\\s*|\t|\r|\n");Matcher m = p.matcher(str);dest = m.replaceAll("");}return dest;}/*** 判断给定的字符串是否为null或者是空的* @param string 给定的字符串*/public static boolean isEmpty(String string) {return string == null || "".equals(string.trim());}/*** 判断给定的字符串是否不为null且不为空* @param string 给定的字符串*/public static boolean isNotEmpty(String string) {return !isEmpty(string);}/*** 判断给定的字符串数组中的所有字符串是否都为null或者是空的* @param strings 给定的字符串*/public static boolean isEmpty(String... strings) {boolean result = true;for (String string : strings) {if (isNotEmpty(string)) {result = false;break;}}return result;}/*** 判断给定的字符串数组中是否全部都不为null且不为空** @param strings 给定的字符串数组* @return 是否全部都不为null且不为空*/public static boolean isNotEmpty(String... strings) {boolean result = true;for (String string : strings) {if (isEmpty(string)) {result = false;break;}}return result;}/*** 如果字符串是null或者空就返回""*/public static String filterEmpty(String string) {return StringUtil.isNotEmpty(string) ? string : "";}/*** 在给定的字符串中,用新的字符替换所有旧的字符* @param string 给定的字符串* @param oldchar 旧的字符* @param newchar 新的字符* @return 替换后的字符串*/public static String replace(String string, char oldchar, char newchar) {char chars[] = string.toCharArray();for (int w = 0; w < chars.length; w++) {if (chars[w] == oldchar) {chars[w] = newchar;break;}}return new String(chars);}/*** 把给定的字符串用给定的字符分割* @param string 给定的字符串* @param ch 给定的字符* @return 分割后的字符串数组*/public static String[] split(String string, char ch) {ArrayList<String> stringList = new ArrayList<String>();char chars[] = string.toCharArray();int nextStart = 0;for (int w = 0; w < chars.length; w++) {if (ch == chars[w]) {stringList.add(new String(chars, nextStart, w - nextStart));nextStart = w + 1;if (nextStart ==chars.length) {    //当最后一位是分割符的话,就再添加一个空的字符串到分割数组中去stringList.add("");}}}if (nextStart <chars.length) {    //如果最后一位不是分隔符的话,就将最后一个分割符到最后一个字符中间的左右字符串作为一个字符串添加到分割数组中去stringList.add(new String(chars, nextStart,chars.length - 1 - nextStart + 1));}return stringList.toArray(new String[stringList.size()]);}/*** 计算给定的字符串的长度,计算规则是:一个汉字的长度为2,一个字符的长度为1** @param string 给定的字符串* @return 长度*/public static int countLength(String string) {int length = 0;char[] chars = string.toCharArray();for (int w = 0; w < string.length(); w++) {char ch = chars[w];if (ch >= '\u0391' && ch <= '\uFFE5') {length++;length++;}else {length++;}}return length;}private static char[] getChars(char[] chars, int startIndex) {int endIndex = startIndex + 1;//如果第一个是数字if (Character.isDigit(chars[startIndex])) {//如果下一个是数字while (endIndex < chars.length &&Character.isDigit(chars[endIndex])) {endIndex++;}}char[] resultChars = new char[endIndex - startIndex];System.arraycopy(chars, startIndex, resultChars, 0, resultChars.length);return resultChars;}/*** 是否全是数字*/public static boolean isAllDigital(char[] chars) {boolean result = true;for (int w = 0; w < chars.length; w++) {if (!Character.isDigit(chars[w])) {result = false;break;}}return result;}/*** 删除给定字符串中所有的旧的字符** @param string 源字符串* @param ch 要删除的字符* @return 删除后的字符串*/public static String removeChar(String string, char ch) {StringBuffer sb = new StringBuffer();for (char cha : string.toCharArray()) {if (cha != '-') {sb.append(cha);}}return sb.toString();}/*** 删除给定字符串中给定位置处的字符** @param string 给定字符串* @param index 给定位置*/public static String removeChar(String string, int index) {String result = null;char[] chars = string.toCharArray();if (index == 0) {result = new String(chars, 1, chars.length - 1);}else if (index == chars.length - 1) {result = new String(chars, 0, chars.length - 1);}else {result = new String(chars, 0, index) +new String(chars, index + 1, chars.length - index);;}return result;}/*** 删除给定字符串中给定位置处的字符** @param string 给定字符串* @param index 给定位置* @param ch 如果同给定位置处的字符相同,则将给定位置处的字符删除*/public static String removeChar(String string, int index, char ch) {String result = null;char[] chars = string.toCharArray();if (chars.length > 0 && chars[index] == ch) {if (index == 0) {result = new String(chars, 1, chars.length - 1);}else if (index == chars.length - 1) {result = new String(chars, 0, chars.length - 1);}else {result = new String(chars, 0, index) +new String(chars, index + 1, chars.length - index);;}}else {result = string;}return result;}/*** 对给定的字符串进行空白过滤** @param string 给定的字符串* @return 如果给定的字符串是一个空白字符串,那么返回null;否则返回本身。*/public static String filterBlank(String string) {if ("".equals(string)) {return null;}else {return string;}}/*** 将给定字符串中给定的区域的字符转换成小写** @param str 给定字符串中* @param beginIndex 开始索引(包括)* @param endIndex 结束索引(不包括)* @return 新的字符串*/public static String toLowerCase(String str, int beginIndex, int endIndex) {return str.replaceFirst(str.substring(beginIndex, endIndex),str.substring(beginIndex, endIndex).toLowerCase(Locale.getDefault()));}/*** 将给定字符串中给定的区域的字符转换成大写** @param str 给定字符串中* @param beginIndex 开始索引(包括)* @param endIndex 结束索引(不包括)* @return 新的字符串*/public static String toUpperCase(String str, int beginIndex, int endIndex) {return str.replaceFirst(str.substring(beginIndex, endIndex),str.substring(beginIndex, endIndex).toUpperCase(Locale.getDefault()));}/*** 将给定字符串的首字母转为小写** @param str 给定字符串* @return 新的字符串*/public static String firstLetterToLowerCase(String str) {return toLowerCase(str, 0, 1);}/*** 将给定字符串的首字母转为大写** @param str 给定字符串* @return 新的字符串*/public static String firstLetterToUpperCase(String str) {return toUpperCase(str, 0, 1);}/*** 将给定的字符串MD5加密** @param string 给定的字符串* @return MD5加密后生成的字符串*/public static String MD5(String string) {String result = null;try {char[] charArray = string.toCharArray();byte[] byteArray = new byte[charArray.length];for (int i = 0; i < charArray.length; i++) {byteArray[i] = (byte) charArray[i];}StringBuffer hexValue = new StringBuffer();byte[] md5Bytes = MessageDigest.getInstance("MD5").digest(byteArray);for (int i = 0; i < md5Bytes.length; i++) {int val = ((int) md5Bytes[i]) & 0xff;if (val < 16) {hexValue.append("0");}hexValue.append(Integer.toHexString(val));}result = hexValue.toString();} catch (Exception e) {e.printStackTrace();}return result;}/*** 判断给定的字符串是否以一个特定的字符串开头,忽略大小写** @param sourceString 给定的字符串* @param newString 一个特定的字符串*/public static boolean startsWithIgnoreCase(String sourceString, String newString) {int newLength = newString.length();int sourceLength = sourceString.length();if (newLength == sourceLength) {return newString.equalsIgnoreCase(sourceString);}else if (newLength < sourceLength) {char[] newChars = new char[newLength];sourceString.getChars(0, newLength, newChars, 0);return newString.equalsIgnoreCase(String.valueOf(newChars));}else {return false;}}/*** 判断给定的字符串是否以一个特定的字符串结尾,忽略大小写** @param sourceString 给定的字符串* @param newString 一个特定的字符串*/public static boolean endsWithIgnoreCase(String sourceString, String newString) {int newLength = newString.length();int sourceLength = sourceString.length();if (newLength == sourceLength) {return newString.equalsIgnoreCase(sourceString);}else if (newLength < sourceLength) {char[] newChars = new char[newLength];sourceString.getChars(sourceLength - newLength, sourceLength,newChars, 0);return newString.equalsIgnoreCase(String.valueOf(newChars));}else {return false;}}/*** 检查字符串长度,如果字符串的长度超过maxLength,就截取前maxLength个字符串并在末尾拼上appendString*/public static String checkLength(String string, int maxLength, String appendString) {if (string.length() > maxLength) {string = string.substring(0, maxLength);if (appendString != null) {string += appendString;}}return string;}/*** 检查字符串长度,如果字符串的长度超过maxLength,就截取前maxLength个字符串并在末尾拼上…*/public static String checkLength(String string, int maxLength) {return checkLength(string, maxLength, "…");}
}

StringUtil工具类:相关推荐

  1. StringUtil工具类

    1.字符串达到多长才截取 2.将指定的对象转换为String类型 3.转换字符,用于替换提交的数据中存在非法数据:"'" 4.对标题""转换为中文"& ...

  2. StringUtil工具类详解

    org.apache.commons.lang.StringUtils中方法的操作对象是java.lang.String类型的对象,是JDK提供的String类型操作方法的补充,并且是null安全的( ...

  3. java操作字符串的工具类StringUtil

    依赖引入 <dependency><groupId>commons-lang</groupId><artifactId>commons-lang< ...

  4. java StringUtil之String工具类

    StringUtil之String工具类 获取字典字符串中的value <功能详细描述> public static final String dictToValue(String mSt ...

  5. StringUtil:字符串处理的工具类

    一个字符串处理的工具类(●'◡'●) 包含以下功能: 判断应用程序是否安装 字符拆分成数组 MD5 加密 拼接后的字符串 替换字符串 判断多个参数是否都为空 将字符串转换成HTML格式的字符串 将HT ...

  6. 可执行SQL文的mybatis工具类

    1.创建工具类  import org.apache.ibatis.builder.StaticSqlSource; import org.apache.ibatis.exceptions.TooMa ...

  7. 字符串工具类、数组工具类、集合工具类、转型操作工具类、编码与解码操作工具类...

    package hjp.smart4j.framework.util;import org.apache.commons.lang3.StringUtils;/*** 字符串工具类*/ public ...

  8. 一个基于POI的通用excel导入导出工具类的简单实现及使用方法

    前言: 最近PM来了一个需求,简单来说就是在录入数据时一条一条插入到系统显得非常麻烦,让我实现一个直接通过excel导入的方法一次性录入所有数据.网上关于excel导入导出的例子很多,但大多相互借鉴. ...

  9. 实现-驼峰和下划线的转换 工具类

    /*** 工具类-驼峰和下划线的转换*/ public class StringUtil {/*** 下划线命名转驼峰命名* @param underscore* @return*/public st ...

  10. 程序员你为什么这么累【续】:编码习惯之工具类规范

    导读: 程序员你为什么这么累? 我的编码习惯 - 接口定义 我的编码习惯 - Controller规范 我的编码习惯 - 日志建议 我的编码习惯 - 异常处理 我的编码习惯 - 参数校验和国际化规范 ...

最新文章

  1. linux中用gtk编写的聊天室能运行的,CHAT_ROOM
  2. GA(遗传算法)的Matlab程序原理(from:六分之一工作室)
  3. 共享思维导图,协作型思维导图Leangoo
  4. ASP.NET服务器端控件原理分析
  5. SQL Server系统存储过程
  6. SQL存储过程跨服务器访问
  7. 2005年1月8日——最伤心的一天
  8. oracle 登录dba,在Oracle10gisqlplus下登录dba用户
  9. [转]灯灯小程序开发手记:仿今日头条(上)
  10. python交通流预测代码,使用python进行交通流量预测
  11. MTK LED驱动异常检测步骤
  12. firefox插件使用:hackbar
  13. springboot2.2.6文件上传、下载及文件超出大小限制的处理
  14. 软件定义边界(SDP)简介
  15. TweenMax介绍
  16. esxi远程管理端口_如何在 vmware esxi 中开放 VNC功能及端口实现远程管理 完整篇...
  17. 密集预测任务的多任务学习综述
  18. FCPX插件:Stupid Raisins Block Pop(视频转场插件)
  19. 用户开启了iCloud 照片库,选择了“优化 iPhone/iPad 储存空间”获取图片失败
  20. 中小服装企业应用ERP软件的问题及解决方法

热门文章

  1. 用c语言写图书管理系统设计,C语言图书管理系统设计及实现.doc
  2. julia语言 python解释器_深入Python解释器源码,我终于搞明白了字符串驻留的原理...
  3. java+widthstep_关于IplImage中widthstep的大小与width,nchannels等的关系的问题
  4. 数据库修改服务器,服务器数据库修改
  5. CSND的Markdown使用练习
  6. Linux 操作系统原理 — 操作系统的本质
  7. 二级C语言考前学习资料(机试)及C语言程序二十四种大题题型
  8. fruit loops studio音乐宿主软件daw水果软件20.9中文版
  9. Android开发:申请小米开发者账号步骤
  10. 自学-Linux-老男孩Linux77期-day4