Files
usdt/luqj-common/src/main/java/com/luqj/util/StringUtils.java
T
2026-03-03 09:39:33 +08:00

1870 lines
45 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* 版权所有:厦门市巨龙软件工程有限公司
* Copyright 2009 Xiamen Dragon Software Eng. Co. Ltd.
* All right reserved.
*====================================================
* 文件名称: StringUtils.java
* 修订记录:
* No 日期 作者(操作:具体内容)
* 1. Mar 3, 2009 何志昭(创建:创建文件)
* 2004-11-16 陈雄华更改了splitString方法、增加了toVisualString方法、增加了subStringCount方法
* 2. Mar 3, 2009 根据dragonsoft_checks.xml规范进行调整
*====================================================
* 类描述:
*
*/
package com.luqj.util;
import java.security.MessageDigest;
import java.sql.Date;
import java.sql.Time;
import java.sql.Timestamp;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* <pre>
* 字符串处理工具类.
* &lt;blockquote&gt;
* &lt;b&gt;数字格式化样式&lt;/b&gt;
* &lt;table border=0 cellspacing=3 cellpadding=0 width=&quot;90%&quot;&gt;
* <tr bgcolor="#ccccff">
* <th align=left>
* Symbol
* </th>
* <th align=left>
* Location
* </th>
* <th align=left>
* Localized
* </th>
* <th align=left>
* Meaning
* </th>
* </tr>
* <tr valign=top>
* <td><code>
* 0</code></td>
* <td>
* Number
* </td>
* <td>
* Yes
* </td>
* <td>
* Digit
* </td>
* </tr>
* <tr valign=top bgcolor="#eeeeff">
* <td><code>
* #
* </code></td>
* <td>
* Number
* </td>
* <td>
* Yes
* </td>
* <td>
* Digit, zero shows as absent
* </td>
* </tr>
* <tr valign=top>
* <td><code>
* .
* </code></td>
* <td>
* Number
* </td>
* <td>
* Yes
* </td>
* <td>
* Decimal separator or monetary decimal separator
* </td>
* </tr>
* <tr valign=top bgcolor="#eeeeff">
* <td><code>
* -
* </code></td>
* <td>
* Number
* </td>
* <td>
* Yes
* </td>
* <td>
* Minus sign
* </td>
* </tr>
* <tr valign=top>
* <td><code>
* ,
* </code></td>
* <td>
* Number
* </td>
* <td>
* Yes
* </td>
* <td>
* Grouping separator
* </td>
* </tr>
* <tr valign=top bgcolor="#eeeeff">
* <td><code>
* E</code></td>
* <td>
* Number
* </td>
* <td>
* Yes
* </td>
* <td>
* Separates mantissa and exponent in scientific notation.
* <em>
* Need not be quoted in prefix or suffix.
* </em></td>
* </tr>
* <tr valign=top>
* <td><code>
* ;
* </code></td>
* <td>
* Subpattern boundary
* </td>
* <td>
* Yes
* </td>
* <td>
* Separates positive and negative subpatterns
* </td>
* </tr>
* <tr valign=top bgcolor="#eeeeff">
* <td><code>
* %
* </code></td>
* <td>
* Prefix or suffix
* </td>
* <td>
* Yes
* </td>
* <td>
* Multiply by 100 and show as percentage
* </td>
* </tr>
* <tr valign=top>
* <td><code>
* \u2030
* </code></td>
* <td>
* Prefix or suffix
* </td>
* <td>
* Yes
* </td>
* <td>
* Multiply by 1000 and show as per mille
* </td>
* </tr>
* <tr valign=top bgcolor="#eeeeff">
* <td><code>
* ¤</code>
* (
* <code>
* \u00A4
* </code>
* )
* </td>
* <td>
* Prefix or suffix
* </td>
* <td>
* No
* </td>
* <td>
* Currency sign, replaced by currency symbol. If
* doubled, replaced by international currency symbol.
* If present in a pattern, the monetary decimal separator
* is used instead of the decimal separator.
* </td>
* </tr>
* <tr valign=top>
* <td><code>
* '
* </code></td>
* <td>
* Prefix or suffix
* </td>
* <td>
* No
* </td>
* <td>
* Used to quote special characters in a prefix or suffix,
* for example,
* <code>
* &quot;'#'#&quot;
* </code>
* formats 123 to
* <code>
* &quot;#123&quot;
* </code>
* . To create a single quote
* itself, use two in a row:
* <code>
* &quot;# o''clock&quot;
* </code>
* .
* </td>
* </tr>
* &lt;/table&gt;
*
* &lt;b&gt;符号(Symbol)通常是组合在一起使用的,例如下表&lt;/b&gt;
* &lt;table border=0 cellspacing=3 cellpadding=0 width=&quot;90%&quot;&gt;
* <tr bgcolor="#ccccff">
* <th align=left>
* 输入
* </th>
* <th align=left>
* 格式
* </th>
* <th align=left>
* 输出
* </th>
* </tr>
* <tr valign=top>
* <td rowspan=8>
* 1234.123456
* </td>
* <td><code>
* 0</code></td>
* <td>
* 1234
* </td>
* </tr>
* <tr>
* <td><code>
* 0.00
* </code></td>
* <td>
* 1234.12
* </td>
* </tr>
* <tr>
* <td><code>
* 0.###
* </code></td>
* <td>
* 1234.123
* </td>
* </tr>
* <tr>
* <td><code>
* 0.0000
* </code></td>
* <td>
* 1234.1235
* </td>
* </tr>
* <tr>
* <td><code>
* 00000000.00
* </code></td>
* <td>
* 00001234.12
* </td>
* </tr>
* <tr>
* <td><code>
* ,000.00
* </code></td>
* <td>
* 1,234.12
* </td>
* </tr>
* <tr>
* <td><code>
* ,00.00000000
* </code></td>
* <td>
* 12,34.12345600
* </td>
* </tr>
* <tr>
* <td><code>
* 0.00%
* </code></td>
* <td>
* 123412.35%
* </td>
* </tr>
* <tr valign=top bgcolor="#eeeeff">
* <td rowspan=5>
* 1234.100056
* </td>
* <td><code>
* 0</code></td>
* <td>
* 1234
* </td>
* </tr>
* <tr valign=top bgcolor="#eeeeff">
* <td><code>
* 0.00
* </code></td>
* <td>
* 1234.10
* </td>
* </tr>
* <tr valign=top bgcolor="#eeeeff">
* <td><code>
* 0.##
* </code></td>
* <td>
* 1234.1
* </td>
* </tr>
* <tr valign=top bgcolor="#eeeeff">
* <td><code>
* (人民币),000.00
* </code></td>
* <td>
* (人民币)1,234.10
* </td>
* </tr>
* <tr valign=top bgcolor="#eeeeff">
* <td><code>
* ,000.########
* </code></td>
* <td>
* 1,234.100056
* </td>
* </tr>
* &lt;/table&gt;
* <BR>
* 注:0与#功能类似,区别在于用0表示位数时,格式化后的首尾的0会显示,而用#表示位数时,首尾的0不显示
* &lt;/blockquote&gt;
* </pre>
*
* <BR>
* <DL>
* <DT><B>JDK版本:</B></DT>
* <BR>
* <DD>1.4</DD>
* </DL>
*
* @author 涂传滨,伊新煦,陈雄华,谢计生
* @version 1.0
* @since 1.0
* @see DecimalFormat
*/
public final class StringUtils {
/** 字符长度 */
public static final int CHAR_LEN = 1;
/** 字节长度 */
public static final int BYTE_LEN = 2;
final static SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyyMMddHHmmss");;
public static boolean notBlank(String str) {
return !isBlank(str);
}
public static boolean notBlank(String... strings) {
if (strings == null || strings.length == 0) {
return false;
}
for (String str : strings) {
if (isBlank(str)) {
return false;
}
}
return true;
}
public static boolean hasText(String text) {
if (text == null)
return false;
for (int i = 0; i < text.length(); i++) {
if (!Character.isWhitespace(text.charAt(i)))
return true;
}
return false;
}
public static String parseDateToString(Object obj){
String dateStr = null;
if(obj == null){
return null;
}
if(obj.getClass().getName().equals("java.sql.Date")){
Date date = (Date) obj;
return String.valueOf(date.getTime());
}else if(obj.getClass().getName().equals("java.sql.Timestamp")){
Timestamp date = (Timestamp) obj;
return String.valueOf(date.getTime());
}else if(obj.getClass().getName().equals("java.sql.Time")){
Time date = (Time) obj;
return String.valueOf(date.getTime());
}else{
return String.valueOf(obj);
}
/*if(obj.getClass().getName().equals("java.sql.Date")||
obj.getClass().getName().equals("java.sql.Timestamp")||
obj.getClass().getName().equals("java.sql.Time")){
try {
Date date = (Date) obj;
date.getTime();
dateStr = DATE_FORMAT.format(obj);
} catch (ArrayIndexOutOfBoundsException e) {
// TODO Auto-generated catch block
e.printStackTrace();
logger.error("出现异常的数据"+obj);
throw new ArrayIndexOutOfBoundsException("数组越界"+obj);
}
}else{
return (String)obj;
}
*/
//return dateStr;
}
public static String uuid(){
return UUID.randomUUID().toString().replaceAll("-", "");
}
public static String lowerFirst(String v){
return v.replaceFirst(v.substring(0,1), v.substring(0,1).toLowerCase());
}
public static String upFirst(String v){
return v.replaceFirst(v.substring(0,1), v.substring(0,1).toUpperCase());
}
/**
* 数字字符串转化为整数
*
* @param intStr
* String 待转化的数字字符串
* @param intDef
* int 当intStr为空或空字符串时返回的缺省值
* @return int 返回由数字字符串转化成的数字,当intStr为空或空字符串时,返回缺省值intDef
*/
public static int getInt(String intStr, int intDef) {
if (null == intStr || "".equals(intStr.trim())) {
return intDef;
}
int intRetu = intDef;
Double db = new Double(intStr);
intRetu = db.intValue();
return intRetu;
}
/**
* 数字字符串转化为整数,在转化时发生异常,则返回0
*
* @param intStr
* String 待转化的数字字符串
* @return int 返回由数字字符串转化成的整数,当intStr为空或空字符串时,返回0
*/
public static int getInt(String intStr) {
return getInt(intStr, 0);
}
/**
* 数字字符串转化为双精数字
*
* @param dbstr
* String 待转化的数字字符串
* @param dbDef
* double 当dbstr为空或空字符串时返回的缺省值
* @return double 返回由数字字符串转化成的数字,当dbstr为空或空字符串时,则返回缺省值dbDef
*/
public static double getDouble(String dbstr, double dbDef) {
if (null == dbstr || "".equals(dbstr.trim())) {
return dbDef;
}
double dbRetu = dbDef;
Double db = new Double(dbstr);
dbRetu = db.doubleValue();
return dbRetu;
}
/**
* 数字字符串转化为双精数字,在转化时发生异常,则返回0
*
* @param dbstr
* String 待转化的数字字符串
* @return double 返回由数字字符串转化成的数字,当dbstr为空或空字符串时,则返回0。
*/
public static double getDouble(String dbstr) {
return getDouble(dbstr, 0);
}
/**
* 数字字符串转化为长整型
*
* @param longstr
* String 待转化的数字字符串
* @param longDef
* long 当longstr为空或空字符串时返回的缺省值
* @return long 返回由数字字符串转化成的数字,当longstr为空或空字符串时,则返回缺省值longDef
*/
public static long getLong(String longstr, long longDef) {
if (null == longstr || "".equals(longstr.trim())) {
return longDef;
}
long longRetu = longDef;
Double db = new Double(longstr);
longRetu = db.longValue();
return longRetu;
}
/**
* 数字字符串转化为长整型,在转化时发生异常,则返回0
*
* @param longstr
* String 待转化的数字字符串
* @return long 返回由数字字符串转化成的数字,当longstr为空或空字符串时,则返回0。
*/
public static long getLong(String longstr) {
return getLong(longstr, 0);
}
/**
* 字符串转化为布尔型
*
* @param booleanstr
* String 待转化的字符串
* @param booleanDef
* boolean 当字符串为空或为null时返回的值
* @return boolean 返回转化化的布尔值,只有当booleanstr为字符串“true”(忽略大小写)时才返回true, <BR>
* 如果在转化时发生异常(只有为null时),则返回缺省值booleanDef
*/
public static boolean getBoolean(String booleanstr, boolean booleanDef) {
if (null == booleanstr) {
return booleanDef;
}
boolean booleanRetu = booleanDef;
if ("true".equalsIgnoreCase(booleanstr.trim())) {
booleanRetu = true;
}
return booleanRetu;
}
/**
* 字符串转化为布尔型. <BR>
* 只有当booleanstr为字符串“true”(忽略大小写)时才返回true,其它都返回false, 包括booleanstr为null
*
* @param booleanstr
* String 待转化的字符串
* @return boolean 返回转化化的布尔值,
*/
public static boolean getBoolean(String booleanstr) {
return getBoolean(booleanstr, false);
}
/**
* 双精数字格式化,返回字符串
*
* @param db
* double 待格式化的双精数字
* @param fmt
* String 格式化样式,参见类说明, <BR>
* fmt非法时,db将按原样转化为字符串后返回。
* @return String 格式化后的数字,以字符串方式返回
*/
public static String getNumFormat(double db, String fmt) {
String retu = "";
if (null == fmt || "".equals(fmt.trim())) {
return Double.toString(db);
}
try {
DecimalFormat decimalformat = new DecimalFormat(fmt);
retu = decimalformat.format(db);
decimalformat = null;
} catch (IllegalArgumentException iaex) {
// ialog.error(ex.getMessage());
retu = Double.toString(db);
}
return retu;
}
/**
* 双精数字格式化,把入参中的双精数格式化为带两位小数的数字字符串
*
* @param db
* double 待格式化的双精数字
* @return String 格式化为两位小数的数字字符串
*/
public static String getNumFormat(double db) {
return getNumFormat(db, "0.00");
}
/**
* 数字字符串格式化,返回字符串
*
* @param numStr
* String 待格式化的数字字符串, <BR>
* 如果numStr参数不是的数字则不进行格式化,按原样返回
* @param fmt
* String 格式化样式,参见类说明 <BR>
* fmt非法时,将把numStr按原样返回。
* @return String 格式化后的字符串
*/
public static String getNumFormat(String numStr, String fmt) {
double db = getDouble(numStr, 0);
return getNumFormat(db, fmt);
}
/**
* 数字字符串格式化,把入参中的数字字符串格式化为带两位小数的数字字符串
*
* @param numStr
* String 待格式化的数字字符串, <BR>
* 如果numStr参数不是的数字则不进行格式化,按原样返回
* @return String 格式化为两位小数的数字字符串
*/
public static String getNumFormat(String numStr) {
return getNumFormat(numStr, "0.00");
}
/**
* 普通字符串转化为网页中可显示的,如回车转化为&lt;br&gt;.
*
* @param str
* String 待转化的字符串
* @return String 转化后的字符串
*/
public static String htmlEncode(String str) {
String retu = null;
if (null == str || "".equals(str.trim())) {
retu = str;
} else {
String html = str;
//StringUtils tool = new StringUtils();
html = replaceString(html, "&", "&amp;");
html = replaceString(html, "<", "&lt;");
html = replaceString(html, ">", "&gt;");
html = replaceString(html, "\r\n", "\n");
html = replaceString(html, "\n", "<br>");
html = replaceString(html, "\t", " ");
html = replaceString(html, " ", "&nbsp;");
html = replaceString(html, "\"", "&quot;");
retu = html;
html = null;
}
return retu;
}
/**
* 字符串替换,把str字符串中的所有oldStr子串替换为newStr字串
*
* @param sourceStr
* String 将被替换的字符串,为null时不执行任何替换操作
* @param oldStr
* String 将被替换的子串,为null或为空字符串时不执行替换操作
* @param newStr
* String 将被替换成的子串,为null时不执行替换操作
* @param isIgnoreCase
* boolean 是否忽略大小写,true表忽略大小写,false表大小写敏感。
* @return String 替换后的字符串
*/
public static String replaceString(String sourceStr, String oldStr, String newStr, boolean isIgnoreCase) {
if (sourceStr == null || oldStr == null || oldStr.equals("")) {
return null;
}
String strRetStr = sourceStr;
int pos = strRetStr.indexOf(oldStr);
while (pos != -1) {
strRetStr = strRetStr.substring(0, pos) + newStr + strRetStr.substring(pos + oldStr.length());
pos = strRetStr.indexOf(oldStr, pos + newStr.length());
}
return strRetStr;
}
/**
* 字符串替换,把str字符串中的所有oldStr子串替换为newStr字串(大小写敏感)
*
* @param sourceStr
* String 将被替换的字符串,为null时不执行任何替换操作
* @param oldStr
* String 将被替换的子串,为null或为空字符串时不执行替换操作
* @param newStr
* String 将被替换成的子串,为null时不执行替换操作
* @return String 替换后的字符串
*/
public static String replaceString(String sourceStr, String oldStr, String newStr) {
return replaceString(sourceStr, oldStr, newStr, false);
}
/**
* 字符串按指定分隔符分解为一个数组(大小写敏感). <BR>
* 例子: <BR>
* String[] array = StringUtils.splictString("AA/BBB/CCC//DDDD//" , "/" );
* <BR>
* 结果: <BR>
* array[0]="AA" array[1]="BBB" array[2]="CCC" array[3]="DDDD"
*
* @param sourceStr
* String 源字符串
* @param splitStr
* String 分隔符
* @param isTrim
* boolean
* @return String[] 字符串数组, <BR>
* 源字符串为null或为空字符串时,返回长度为1的数组,元素值为空字符串 <BR>
* 分隔符为null或为空字符串时,返回长度为1的数组,元素值为源字符串
*/
public static String[] splitString(String sourceStr, String splitStr, boolean isTrim) {
String newSourceStr = sourceStr;
String newSplitStr = splitStr;
if (newSourceStr == null || newSplitStr == null) {
return null;
}
if (isTrim) // 去掉开始和结束的分隔符
{
// 开头的间隔符去掉
while (newSourceStr.indexOf(newSplitStr) == 0) {
newSourceStr = newSourceStr.substring(newSplitStr.length());
}
// 尾部的间隔符去掉
while (newSourceStr.indexOf(newSplitStr, newSourceStr.length() - newSplitStr.length()) > -1) {
newSourceStr = newSourceStr.substring(0, sourceStr.length() - newSplitStr.length());
}
}
if (newSourceStr.equals("") || newSplitStr.equals("")) {
return null;
}
return splitString(newSourceStr, newSplitStr);
}
/**
* 功能简述 :把字符串放入一个数组
*
* @param sourceStr
* 要被放入的字符串
* @param splitStr
* 间隔符
* @return 转换后的数组,sourceStr或splitStr = null 或 "" 返回null
*/
public static String[] splitString(String sourceStr, String splitStr) {
String newSourceStr = sourceStr;
String newSplitStr = splitStr;
if (newSourceStr == null || newSplitStr == null) {
return null;
}
if (newSourceStr.equals("") || newSplitStr.equals("")) {
return null;
}
int intArraySize = subStringCount(newSourceStr, newSplitStr);
// 如果为-1则返回
if (intArraySize == -1) {
return null;
}
newSourceStr += newSplitStr;
String[] strRetArr = new String[intArraySize + 1];
int intpos = newSourceStr.indexOf(newSplitStr);
int intArrayPos = 0;
while (intpos != -1) {
strRetArr[intArrayPos++] = newSourceStr.substring(0, intpos);
newSourceStr = newSourceStr.substring(intpos + newSplitStr.length());
intpos = newSourceStr.indexOf(newSplitStr);
}
return strRetArr;
}
/**
* 功能简述 :把字符串放入一个数组
* @param sourceStr
* 要被放入的字符串
* @param splitStr
* 间隔符
* @return 转换后的List,sourceStr或splitStr = null 或 "" 返回元素个数为0的List
*/
public static List splitString2List(String sourceStr, String splitStr) {
String[] tempArr = splitString(sourceStr, splitStr);
List resultList = new ArrayList();
if (tempArr != null) {
for (int i = 0; i < tempArr.length; i++) {
resultList.add(tempArr[i]);
}
}
return resultList;
}
/**
* 功能简述 :把字符串放入一个数组
*
* @param sourceStr
* 要被放入的字符串
* @param splitStr
* 间隔符
* @param isTrim
* boolean
* @return 转换后的List,sourceStr或splitStr = null 或 "" 返回元素个数为0的List
*/
public static List splitString2List(String sourceStr, String splitStr, boolean isTrim) {
String[] tempArr = splitString(sourceStr, splitStr, isTrim);
List resultList = new ArrayList();
if (tempArr != null) {
for (int i = 0; i < tempArr.length; i++) {
resultList.add(tempArr[i]);
}
}
return resultList;
}
/**
* 功能简述 :在一个字符串中查找字符串个数(不区分大小写)
*
* @param sourceStr
* 要被查询的字符串
* @param subStr
* 要查找的字符串
* @return 找到的个数
*/
public static int subStringCount(String sourceStr, String subStr) {
// 如果是空指针则返回-1
if (sourceStr == null || subStr == null) {
return -1;
}
if (sourceStr.equals("") || subStr.equals("")) {
return -1;
}
int result = 0;
int intPos = sourceStr.toUpperCase().indexOf(subStr.toUpperCase());
while (intPos != -1) {
result++;
intPos = sourceStr.toUpperCase().indexOf(subStr.toUpperCase(), intPos + subStr.length());
}
return result;
}
/**
* 用指定的分隔符把字符串数组合并成一个字符串. 数组为null或长度为0时返回空字符串 <BR>
* 例子: <BR>
* String[] array = {"1",null,"","3"}; <BR>
* StringUtils.arrayToString(array,"|"); <BR>
* 返回结果:1|||3
*
* @param array
* String[] 待合并的数组
* @param splitStr
* String 数组各元素合并后,它们之间的分隔符,为null时用空字符串代替
* @return String 合并后的字符串
*/
public static String arrayToString(String[] array, String splitStr) {
if (null == array || 0 == array.length) {
return "";
}
String newSplitStr = splitStr;
if (null == newSplitStr) {
newSplitStr = "";
}
StringBuffer strBuf = new StringBuffer("");
int len = array.length;
for (int i = 0; i < len - 1; i++) {
strBuf.append((null == array[i]) ? "" : array[i]).append(newSplitStr);
}
strBuf.append((null == array[len - 1]) ? "" : array[len - 1]);
return strBuf.toString();
}
/**
* 用默认分隔符 | 把字符串数组合并成一个字符串. 数组为null或长度为0时返回空字符串
*
* @param array
* String[] 待合并的数组
* @return String 合并后的字符串
*/
public static String arrayToString(String[] array) {
return arrayToString(array, "|");
}
/**
* 判断字符串是否为null或为空字符串(包括只含空格的字符串)
*
* @param str
* String 待检查的字符串
* @return boolean 如果为null或空字符串(包括只含空格的字符串)则返回true,否则返回false
*/
public static boolean isNullBlank(String str) {
return (null == str || "".equals(str.trim())) ? true : false;
}
/**
* 从字符串中得到指定位置的子串(按分隔符分隔,大小写敏感). <BR>
* 例子: <BR>
* StringUtils.decomposeString("a||b|c|d","|",1); <BR>
* StringUtils.decomposeString("a||b|c|d","|",2); <BR>
* StringUtils.decomposeString("a||b|c|d","|",3); <BR>
* StringUtils.decomposeString("a||b|c|d","|",4); <BR>
* StringUtils.decomposeString("a||b|c|d","|",5); <BR>
* 返回结果: <BR>
* a,空字符串,b,c,d
*
* @param sourceStr
* String 源字符串
* @param splitStr
* String 分隔符
* @param pos
* int 位置,从1开始
* @return String 返回指定位置的子串。 <BR>
* 当分隔符为null或为空字符串时返回源字符串; <BR>
* 当源字符串为null或为空字符串时返回空字符串; <BR>
* 当指定位置小于1或大于分隔符个数-1时返回空字符串。
*/
public static String decomposeString(String sourceStr, String splitStr, int pos) {
String retu = "";
String newSourceStr = sourceStr;
if (null == newSourceStr || "".equals(newSourceStr.trim())) {
return "";
}
if (pos <= 0) {
return "";
}
if (null == splitStr || "".equals(splitStr)) {
return sourceStr;
}
newSourceStr = newSourceStr + splitStr;
String tmpStr = newSourceStr;
int splitLen = splitStr.length();
int tmpLen = tmpStr.length();
for (int i = 0; i < pos - 1; i++) {
int tmpPosi = tmpStr.indexOf(splitStr);
if (tmpPosi < 0 || tmpPosi + splitLen >= tmpLen) {
tmpStr = splitStr;
break;
} else {
tmpStr = tmpStr.substring(tmpPosi + splitLen);
}
}
retu = tmpStr.substring(0, tmpStr.indexOf(splitStr));
return retu;
}
/**
* 从字符串中得到指定位置的子串(按分隔符 | 分隔).
*
* @param sourceStr
* String 源字符串
* @param pos
* int 位置,从1开始
* @return String 返回指定位置的子串。 <BR>
* 当分隔符为null或为空字符串时返回源字符串; <BR>
* 当源字符串为null或为空字符串时返回空字符串; <BR>
* 当指定位置小于1或大于分隔符个数-1时返回空字符串。
*/
public static String decomposeString(String sourceStr, int pos) {
return decomposeString(sourceStr, "|", pos);
}
/**
* 删除指定的前导、后导子串(大小写敏感). <br>
* 例子: <br>
* StringUtils.trim("and and username = '123' and password = 'abc' and
* ","and "); <br>
* 结果:username = '123' and password = 'abc'
*
* @param sourceStr
* String 待删除的字符串
* @param removeChar
* char 子串
* @return String 处理后的字符串
*/
public static String trim(String sourceStr, char removeChar) {
if (sourceStr == null) {
return null;
}
String newSourceStr = sourceStr;
newSourceStr = newSourceStr.trim();
int loIntBegin = 0;
int loIntEnd = 0;
int loIntLen = newSourceStr.length();
for (int i = 0; i < loIntLen; i++) {
if (newSourceStr.charAt(i) == removeChar) {
loIntBegin++;
} else {
break;
}
}
for (int i = 0; i < loIntLen; i++) {
if (newSourceStr.charAt(loIntLen - 1 - i) == removeChar) {
loIntEnd++;
} else {
break;
}
}
return newSourceStr.substring(loIntBegin, loIntLen - loIntEnd);
}
/**
* 删除指定的前导、后导子串(大小写敏感). <br>
* 例子: <br>
* StringUtils.trim("and and username = '123' and password = 'abc' and
* ","and "); <br>
* 结果:username = '123' and password = 'abc'
*
* @param sourceStr
* String 待删除的字符串
* @param removeChar
* char 子串
* @param end
* true 只trim末尾,false 只trim头部
* @return String 处理后的字符串
*/
public static String trim(String sourceStr, char removeChar, boolean end) {
if (sourceStr == null) {
return null;
}
String newSourceStr = sourceStr;
newSourceStr = newSourceStr.trim();
int loIntBegin = 0;
int loIntEnd = 0;
int loIntLen = newSourceStr.length();
for (int i = 0; i < loIntLen; i++) {
if (newSourceStr.charAt(i) == removeChar) {
loIntBegin++;
} else {
break;
}
}
for (int i = 0; i < loIntLen; i++) {
if (newSourceStr.charAt(loIntLen - 1 - i) == removeChar) {
loIntEnd++;
} else {
break;
}
}
if (end) {
return newSourceStr.substring(0, loIntLen - loIntEnd);
} else {
return newSourceStr.substring(loIntBegin);
}
}
/**
* <pre>
* 将字符串截短到特定的长度,一个中文是两个字节的长度,但是是1个字符的长度,例:
* 1)truncate(&quot;巨龙软件公司&quot;,2,CHAR_LEN) = &quot;巨龙&quot;
* 2)truncate(&quot;巨龙软件公司&quot;,2,BYTE_LEN) = &quot;巨&quot;
* 3)truncate(&quot;dragonsoft&quot;,2,CHAR_LEN) = &quot;dr&quot;
* 4)truncate(&quot;dragonsoft&quot;,2,BYTE_LEN) = &quot;dr&quot;
* </pre>
*
* @param str
* String 需要截的字符串
* @param len
* int 截到的目标长度
* @param lenType
* int 长度类型,字符长度{@link CHAR_LEN},字符长度{@link BYTE_LEN}
* @return String
*/
public static String truncate(String str, int len, int lenType) {
if (str == null) {
return str;
}
// 字符长度
if (lenType == CHAR_LEN) {
if (str.length() <= len) {
return str;
} else {
return str.substring(0, len);
}
} else // 字节长度
{
StringBuffer sb = new StringBuffer();
int temp = 0;
for (int i = 0; i < str.length(); i++) {
if (i < len) // 是否到指定的长度
{
temp = ("" + str.charAt(i)).getBytes().length;
if (i + temp <= len) {
sb.append(str.charAt(i));
} else {
break;
}
} else {
break;
}
}
return sb.toString();
}
}
/**
* 从源字符串中从第一个字符开始取出给定长度的字串. <BR>
* 源字符串长度大于len时,尾巴追加一个appendStr串
*
* @param sourceStr
* String 源字符串
* @param len
* int 取出的长度
* @param appendStr
* String 追加的字符串(常用的appendStr为...)
* @return String 取出的子串
*/
public static String substring(String sourceStr, int len, String appendStr) {
if (null == sourceStr || "".equals(sourceStr)) {
return sourceStr;
}
if (len <= 0) {
return "";
}
String newappendStr = appendStr;
if (null == newappendStr) {
newappendStr = "";
}
int sourceLen = sourceStr.length();
if (len >= sourceLen) {
return sourceStr;
} else {
return sourceStr.substring(0, len) + newappendStr;
}
}
/**
* 产生指定长度随机字符串(包括字母及数字).
*
* @param length
* int 随机字符串长度
* @return String 随机字符串
*/
public static String random(int length) {
String retu = "";
int d1;
int d2;
char[] letters = initLetters();
//int[] numbers = initNumbers();
for (int i = 0; i < length; i++) {
// d1 = ((int) (Math.random() * 10) % 2);
d1 = (int) (Math.random() * 10) % 2;
if (d1 == 0) { // use a letter
// d2 = ((int) (Math.random() * 100) % 52);
d2 = (int) (Math.random() * 100) % 52;
retu += letters[d2];
} else if (d1 == 1) { // use a number
retu += (int) (Math.random() * 10);
}
}
return retu;
}
/**
* 产生纯字母的随机字符串
*
* @param length
* int 随机字符串长度
* @return String 随机字符串
*/
public static String randomString(int length) {
String retu = "";
int d2;
char[] letters = initLetters();
for (int i = 0; i < length; i++) {
// d2 = ((int) (Math.random() * 100) % 52);
d2 = (int) (Math.random() * 100) % 52;
retu += letters[d2];
}
return retu;
}
/**
* 产生纯数字随机字符串
*
* @param length
* int 随机字符串长度
* @return String 随机字符串
*/
public static String randomNumber(int length) {
String retu = "";
//int[] numbers = initNumbers();
for (int i = 0; i < length; i++) {
retu += (int) (Math.random() * 10);
}
return retu;
}
/**
* 内部方法,产生字母数组
*
* @return char[]
*/
private static char[] initLetters() {
char[] ca = new char[52];
for (int i = 0; i < 26; i++) {
ca[i] = (char) (65 + i);
}
for (int i = 26; i < 52; i++) {
ca[i] = (char) (71 + i);
}
return ca;
}
/**
* 内部方法,产生数字数组
*
* @return int[]
*/
/*private static int[] initNumbers() {
int[] na = new int[10];
for (int i = 0; i < 10; i++) {
na[i] = i;
}
return na;
}*/
/**
* 对字符串进行加减密处理.
*
* @param source
* String 待加减密处理的字符串
* @param flag
* boolean true表进行加密,false表进行减密
* @return String 处理后的字符串. <BR>
* source为空则返回原字符串source, <BR>
* 如果source中含有中文字符则对这一中文字符不处理.
*/
public static String encrypt(String source, boolean flag) {
if (null == source || "".equals(source.trim())) {
return source;
}
String lsKEY1 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890abcdefghijklmnopqrstuvwxyz~!@#$%^&*(),.;[]{}";
String lsKEY2 = "&*()AD;[BFLCGH]{PQ}EI!JKRYZ1MSTXNO23UV,.Wcd45ef6lm7g@h0a#$nij8ob%^ptvz9ku~qrswxy";
char[] chSource = source.toCharArray();
StringBuffer strBuf = new StringBuffer(chSource.length);
int pos;
int len = chSource.length;
for (int i = 0; i < len; i++) {
if (flag) { // 加密
pos = lsKEY1.indexOf(chSource[i]);
if (pos >= 0) {
strBuf.append(lsKEY2.substring(pos, pos + 1));
} else {
strBuf.append(chSource[i]);
}
} else { // 减密
pos = lsKEY2.indexOf(chSource[i]);
if (pos >= 0) {
strBuf.append(lsKEY1.substring(pos, pos + 1));
} else {
strBuf.append(chSource[i]);
}
}
}
return strBuf.toString();
}
/**
* 将null字符串转换为空串,方便HTML的显示
*
* @param sourceStr
* 待处理的字符串
* @return String 处理的的字符串
*/
public static String toVisualString(String sourceStr) {
if (sourceStr == null || sourceStr.equals("")) {
return "";
} else {
return sourceStr;
}
}
/**
* 将字段填充到指定的长度
*
* @param sourceStr
* String 操作字符串
* @param length
* int 指定长度
* @param withChar
* char 填充的字符
* @param isPadToEnd
* boolean 是否填充在字符的尾部 true :是 ,false:填充在头部
* @return String
*/
public static String pad(String sourceStr, int length, char withChar, boolean isPadToEnd) {
if (sourceStr == null) {
return null;
}
if (sourceStr.length() >= length) {
return sourceStr;
}
StringBuffer sb = new StringBuffer(sourceStr);
for (int i = 0; i < length - sourceStr.length(); i++) {
if (isPadToEnd) {
sb.append(withChar);
} else {
sb.insert(0, withChar);
}
}
return sb.toString();
}
/**
* 首字母大写
* @return
*/
public static String capToUpper(String str){
if (str == null) {
return null;
}
if(str.length()<0){
return "";
}
return str.substring(0,1).toUpperCase()+str.substring(1);
}
/**
* main
*
* @param args
* String[]
* @author:Administrator
*/
/**
* 将身份证号码转换为18位
*
* @param xIdCode
* String
* @return String
*/
public static String idTo18(String xIdCode) {
return IdentificationNumberUtils.id15ToId18(xIdCode);
}
/**
* 将身份证号转换为15位
*
* @param xIdCode
* String
* @return String
*/
public static String idTo15(String xIdCode) {
return IdentificationNumberUtils.id18ToId15(xIdCode);
}
/**
* getIdCheckBit
*
* @param xId
* id
* @return String
* @author:Administrator
*/
private static String getIdCheckBit(String xId) {
return IdentificationNumberUtils.getIdCheckBit(xId);
}
/**
* MD5加密算法
*
* @更新时间:Apr 22, 2008
* @更新作者:Ryan Wong
* @param s
* String
* @return String
*/
public static String md5(String s) {
char[] hexDigits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
try {
byte[] strTemp = s.getBytes();
MessageDigest mdTemp = MessageDigest.getInstance("MD5");
mdTemp.update(strTemp);
byte[] md = mdTemp.digest();
int j = md.length;
char[] str = new char[j * 2];
int k = 0;
for (int i = 0; i < j; i++) {
byte byte0 = md[i];
str[k++] = hexDigits[byte0 >>> 4 & 0xf];
str[k++] = hexDigits[byte0 & 0xf];
}
return new String(str);
} catch (Exception e) {
return null;
}
}
/**
* 取得字符串中间的几位
*
* @更新时间:Apr 22, 2008
* @更新作者:Ryan Wong
* @param strsource
* str_source
* @param midlength
* midlength
* @return String
*/
public static String getMidString(String strsource, int midlength) {
String strMid = "";
int length = strsource.length();
if (midlength > length) {
return null;
}
if (length % 2 == 0) {
if (midlength % 2 != 0) {
return null;
}
strMid = strsource.substring(length / 2 - midlength / 2, length / 2 + midlength / 2);
} else {
if (midlength % 2 == 0) {
return null;
}
strMid = strsource.substring((length - 1) / 2 - (midlength - 1) / 2, (length - 1) / 2
+ (midlength - 1) / 2 + 1);
}
return strMid;
}
public static boolean isNotBlank(String value){
return (value!=null) && !value.equals("");
}
public static boolean isBlank(String value){
return (value==null) || value.equals("");
}
/**
* getStringToInt
* @param value String
* @param defaultInt int
* @return int
* @author:Administrator
*/
public static int getStringToInt(String value, int defaultInt) {
int i = defaultInt;
if (isNotBlank(value) ) {
Integer intObject = getStringByInt(value);
if(intObject!=null){
return intObject.intValue();
}
}
return i;
}
/**
* 字符串替换
* @param value String
* @param replaceChar String
* @return String
* @author:Administrator
*/
public static String replaceStr(String value, String replaceChar) {
String returnValue = "";
if (isNotBlank(value)) {
returnValue = value.replaceAll("[" + replaceChar + "]", "");
}
return returnValue;
}
/**
* getNullByString
* @param value String
* @return String
* @author:Administrator
*/
public static String getNullByString(String value) {
String values = "";
if (isNotBlank(value)) {
values = value.trim();
}
return values;
}
/**
* getNullByString
* @param value String
* @param defaultValue String
* @return String
* @author:Administrator
*/
public static String getNullByString(String value, String defaultValue) {
String values = "";
if (isNotBlank(value)) {
values = value;
} else {
values = defaultValue;
}
return values;
}
/**
* getStringByInt
* @param value String
* @return Integer
* @author:Administrator
*/
public static Integer getStringByInt(String value) {
Integer intValue = null;
if (isNotBlank(value) ) {
try{
intValue = Integer.valueOf(value);
} catch(NumberFormatException ex){
}
}
return intValue;
}
/**
* getStringByMaxLen
* @param value String
* @param maxLen int
* @return String
* @author:Administrator
*/
public static String getStringByMaxLen(String value, int maxLen) {
String returnValue = "";
if (isNotBlank(value)) {
returnValue = value.length() > maxLen ? value.substring(0, maxLen) : value;
}
return returnValue;
}
/**
* getCheckBoxValueByStr
* @param value String
* @return String
* @author:Administrator
*/
public static String getCheckBoxValueByStr(String value) {
String returnValue = "";
if (isNotBlank(value)) {
if (value.equals("on")) {
returnValue = "1";
} else {
returnValue = "0";
}
}
return returnValue;
}
/**
* 删除字符两端的空字符串
*
* @author cailj
* @param str
* @return
*/
public static String trim(String str) {
return str != null ? str.trim() : null;
}
/**
* 进行字符规格化(全角转半角,大写转小写处理)
* @param input
* @return char
*/
public static char[] regularize(char[] input){
for(int i = 0; i < input.length; i ++){
input[i] = regularize(input[i]);
}
return input;
}
/**
* 进行字符规格化(全角转半角,大写转小写处理)
* @param input
* @return char
*/
public static char regularize(char input){
if (input == 12288) {
input = (char) 32;
}else if (input > 65280 && input < 65375) {
input = (char) (input - 65248);
}else if (input >= 'A' && input <= 'Z') {
input |= 32;
}
return input;
}
/**
* 进行字符规格化(全角转半角,大写转小写处理)
* @param input
* @return char
*/
public static String regularize(String input){
if(StringUtils.isBlank(input)) return "";
char[] inputChars = input.toCharArray();
for(int i = 0; i < inputChars.length; i ++){
inputChars[i] = regularize(inputChars[i]);
}
return new String(inputChars);
}
/**
* 字符反转
* eg: reverse(abc) = cba
* @param str
* @return
*/
public static String reverse(String str) {
char[] chrs = str.toCharArray();
int n = chrs.length - 1;
for (int j = (n-1) >> 1; j >= 0; --j) {
char temp = chrs[j];
char temp2 = chrs[n - j];
chrs[j] = temp2;
chrs[n - j] = temp;
}
return new String(chrs, 0, chrs.length);
}
/**
* 字符串是否是 数字类型
* @param str
* @return
*/
public static boolean isNumber(String str){
if(isInteger(str)||isFloat(str)){
return true;
}else{
return false;
}
}
/**
* 判断字符串是否为浮点型
* @param str
* @return
*/
public static boolean isFloat(String str){
if(StringUtils.isNullBlank(str))return false;
Pattern pattern= Pattern.compile("^[-\\+]?\\d+\\.{0,1}[\\d]+$");
return pattern.matcher(str).matches();
}
/**
* 判断字符串是否是整数
* @param str
* @return
*/
public static boolean isInteger(String str){
if(StringUtils.isNullBlank(str))return false;
Pattern pattern = Pattern.compile("^[-\\+]?[\\d]+$");
return pattern.matcher(str).matches();
}
/**
* 忽略正则关键字
* @param keyword 正在表达式关键字 例如"\t","\d","*"等
* @return 返回正则表达式
*/
public static String ignoreRegexKeyword(String keyword){
return "\\Q"+keyword+"\\E";
}
/**
*
* @param str
* @param character
* @return
*/
public static String removeLastCharacter(String str, String character){
if(str==null||str.length()==0){
return str;
}
int en=-1;
if((en=str.lastIndexOf(character))>=0&&(en+character.length()==str.length())){
return str.substring(0,en);
}else{
return str;
}
}
public static String ignoreKeyword(String txt){
if(txt==null){
return null;
}
String str=ignoreKeyword(txt,"n");
str=ignoreKeyword(str,"t");
return str;
}
public static String ignoreKeyword(Object object){
return ignoreKeyword(String.valueOf(object));
}
public static String ignoreKeyword(String txt, String keyWord){
if(txt==null){
return null;
}
if(txt.length()==0||isBlank(keyWord)){
return txt;
}
return txt.replaceAll("\\"+keyWord,"\\\\"+keyWord);
}
/**
* 下划线转驼峰法
* @param line 源字符串
* @param smallCamel 大小驼峰,是否为小驼峰
* @return 转换后的字符串
*/
public static String underline2Camel(String line, boolean smallCamel){
if(line==null||"".equals(line)){
return "";
}
StringBuffer sb=new StringBuffer();
Pattern pattern= Pattern.compile("([A-Za-z\\d]+)(_)?");
Matcher matcher=pattern.matcher(line);
while(matcher.find()){
String word=matcher.group();
sb.append(smallCamel&&matcher.start()==0? Character.toLowerCase(word.charAt(0)): Character.toUpperCase(word.charAt(0)));
int index=word.lastIndexOf('_');
if(index>0){
sb.append(word.substring(1, index).toLowerCase());
}else{
sb.append(word.substring(1).toLowerCase());
}
}
return sb.toString();
}
/**
* 驼峰法转下划线
* @param line 源字符串
* @return 转换后的字符串
*/
public static String camel2Underline(String line){
if(line==null||"".equals(line)){
return "";
}
line= String.valueOf(line.charAt(0)).toUpperCase().concat(line.substring(1));
StringBuffer sb=new StringBuffer();
Pattern pattern= Pattern.compile("[A-Z]([a-z\\d]+)?");
Matcher matcher=pattern.matcher(line);
while(matcher.find()){
String word=matcher.group();
sb.append(word.toUpperCase());
sb.append(matcher.end()==line.length()?"":"_");
}
return sb.toString();
}
//IOS端version比较大小 param1>param2 返回 1 =返回0 <返回-1
public static int compareVersion(String versionClient,String versionServe){
//版本长度
String[] cArr = versionClient.split("\\.");
String[] sArr = versionServe.split("\\.");
int diff = cArr.length-sArr.length;
int result = 0;
//补齐长度
if(diff>0){
sArr = align(sArr,cArr,diff);
}else{
cArr = align(cArr,sArr,-diff);
}
for(int i=0;i<cArr.length;i++){
String[] param1 = cArr[i].split("");
String[] param2 = sArr[i].split("");
int diff2 = param1.length-param2.length;
//补齐长度
if(diff2>0){
param2 = align(param2,param1,diff2);
}else{
param1 = align(param1,param2,-diff2);
}
int param11 = Integer.parseInt(StringUtils.arrayToString(param1,""));
int param22 = Integer.parseInt(StringUtils.arrayToString(param2,""));
if(param11>param22){
result = 1;
break;
}else if(param11<param22){
result = -1;
break;
}
}
return result;
}
//对齐数组 数组1补到2的长度并返回1 末尾补“0”
public static String[] align(String[] arr1,String[] arr2,int diff){
//补齐长度
String[] arr = new String[arr1.length+diff];
System.arraycopy(arr1,0,arr,0,arr1.length);
for(int i=arr1.length;i<arr.length;i++){
arr[i] = "0";
}
return arr;
}
public static void main(String[] args) {
System.out.println(StringUtils.camel2Underline("ddList"));
}
}