Ver código fonte

重构:删除部分Util,原PubUtil分散到CommUtil和StringUtil、NumberUtil

4.0
郑根木 2 anos atrás
pai
commit
4571518688
4 arquivos alterados com 5 adições e 976 exclusões
  1. +2
    -2
      smtweb-framework/bpm/src/main/java/cc/smtweb/system/bpm/spring/controller/AttachController.java
  2. +3
    -3
      smtweb-framework/bpm/src/main/java/cc/smtweb/system/bpm/spring/file/attach/AttachHelper.java
  3. +0
    -465
      smtweb-framework/bpm/src/main/java/cc/smtweb/system/bpm/util/UtilMath.java
  4. +0
    -506
      smtweb-framework/bpm/src/main/java/cc/smtweb/system/bpm/util/UtilString.java

+ 2
- 2
smtweb-framework/bpm/src/main/java/cc/smtweb/system/bpm/spring/controller/AttachController.java Ver arquivo

@@ -4,6 +4,7 @@ import cc.smtweb.framework.core.common.R;
import cc.smtweb.framework.core.db.DbEngine;
import cc.smtweb.framework.core.util.Consts;
import cc.smtweb.framework.core.util.DateUtil;
import cc.smtweb.framework.core.util.NumberUtil;
import cc.smtweb.framework.core.util.StringUtil;
import cc.smtweb.system.bpm.spring.config.FileConfigProperties;
import cc.smtweb.system.bpm.spring.file.attach.AttachHelper;
@@ -11,7 +12,6 @@ import cc.smtweb.system.bpm.spring.file.attach.AttachUtil;
import cc.smtweb.system.bpm.util.UtilDownloadFile;
import cc.smtweb.system.bpm.util.UtilFile;
import cc.smtweb.system.bpm.util.UtilLogger;
import cc.smtweb.system.bpm.util.UtilMath;
import cc.smtweb.system.bpm.web.sys.base.attach.AttachInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
@@ -372,7 +372,7 @@ public class AttachController {
if (h > height) {
r = Math.min(1.0 * height / h, r);
}
if (!UtilMath.isEqualsZero(r - 1)) {
if (!NumberUtil.isEqualsZero(r - 1)) {
w = (int) Math.round(w * r);
h = (int) Math.round(h * r);
}


+ 3
- 3
smtweb-framework/bpm/src/main/java/cc/smtweb/system/bpm/spring/file/attach/AttachHelper.java Ver arquivo

@@ -3,10 +3,10 @@ package cc.smtweb.system.bpm.spring.file.attach;
import cc.smtweb.framework.core.db.DbEngine;
import cc.smtweb.framework.core.util.CommUtil;
import cc.smtweb.framework.core.util.DateUtil;
import cc.smtweb.framework.core.util.NumberUtil;
import cc.smtweb.framework.core.util.StringUtil;
import cc.smtweb.system.bpm.spring.config.FileConfigProperties;
import cc.smtweb.system.bpm.util.UtilFile;
import cc.smtweb.system.bpm.util.UtilMath;
import cc.smtweb.system.bpm.web.sys.base.attach.AttachInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -228,9 +228,9 @@ public final class AttachHelper {
public String getSizeStr(long fileSize) {
if (fileSize < 1024) return fileSize + "B";
double n = fileSize * 1.0 / 1024;
if (UtilMath.compare(n, 1024) < 0) return UtilMath.toStdNumberString(n, 0) + "KB";
if (NumberUtil.compare(n, 1024) < 0) return NumberUtil.toStdNumberString(n, 0) + "KB";
n = n / 1024;
return UtilMath.toStdNumberString(n) + "MB";
return NumberUtil.toStdNumberString(n) + "MB";
}

/**


+ 0
- 465
smtweb-framework/bpm/src/main/java/cc/smtweb/system/bpm/util/UtilMath.java Ver arquivo

@@ -1,465 +0,0 @@
package cc.smtweb.system.bpm.util;

import cc.smtweb.framework.core.util.StringUtil;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;

/**
* Created with IntelliJ IDEA.
* User: AKhh
* Date: 12-12-25 下午1:02
* To change this template use File | Settings | File Templates.
*/
public class UtilMath {
//整数
private static DecimalFormat dfLng = new DecimalFormat("##############0");
private static DecimalFormat dfLong = new DecimalFormat("###,###,###,###,##0");
//一位小数
private static DecimalFormat df1 = new DecimalFormat("##############0.0");
private static DecimalFormat df1Format = new DecimalFormat("###,###,###,###,##0.0");
//两位小数
private static DecimalFormat df2 = new DecimalFormat("##############0.00");
private static DecimalFormat df2Format = new DecimalFormat("###,###,###,###,##0.00");
//四位小数
private static DecimalFormat df4 = new DecimalFormat("###,###,###,###,##0.0000");
//六位小数
private static DecimalFormat df6Number = new DecimalFormat("#######################0.000000");
private static DecimalFormat df6NumberF = new DecimalFormat("#,###,###,###,###,###,##0.000000");

public final static DecimalFormat stdAmountFormat = new DecimalFormat("###,###,###,###,##0.00");
public final static DecimalFormat stdNumberFormat = new DecimalFormat("#0.00");
public final static String DEF_NUM_TEN_THOUSAND = "10000";//万
public static final double MAX_VALUE = 9999999999999.99D;
public static final double MIN_VALUE = -9999999999999.99D;

private final static BigDecimal ONE_BIG = new BigDecimal(1.00D);
private static final String UNIT = "万仟佰拾亿仟佰拾万仟佰拾元角分";
private static final String DIGIT = "零壹贰叁肆伍陆柒捌玖";

/**
* 4舍5入double,2位小数
*/
public static double roundDouble(double src) {
return roundDouble(src, 2);
}

/**
* 4舍5入double,N 位小数
* @param src
* @param scale 小数位数
* @return
*/
public static double roundDouble(Object src, int scale) {
if (src == null) return 0.0;
String v = src.toString();
if (StringUtil.isEmpty(v)) return 0.0;
if (scale < 0) scale = 2;

BigDecimal src_b = new BigDecimal(v);
BigDecimal src_v = src_b.divide(ONE_BIG, scale + 2, BigDecimal.ROUND_HALF_UP);// 4舍5入
src_v = src_v.divide(ONE_BIG, scale, BigDecimal.ROUND_HALF_UP);// 4舍5入
return src_v.doubleValue();
}

/**
* 舍位处理,原生floor有坑,部分浮点数记录为.99999999999形式,导致floor结果错误
* @param d
* @return
*/
public static double floor(double d) {
return Math.floor(UtilMath.roundDouble(d, 2));
}

/**
* 比较两Double是否相等,将会吧他们专程BigDecimal进行比较;
*
* @param src double1
* @param tag double2
* @return src > tag 返回1, src < tag 返回-1, 否则返回0
*/
public static int compare(double src, double tag) {
BigDecimal src_b = new BigDecimal(src);
BigDecimal src_v = src_b.divide(ONE_BIG, 2, BigDecimal.ROUND_HALF_UP);// 4舍5入

BigDecimal tag_b = new BigDecimal(tag);
BigDecimal tag_v = tag_b.divide(ONE_BIG, 2, BigDecimal.ROUND_HALF_UP);// 4舍5入

return src_v.compareTo(tag_v);
}

/**
* 自动过滤金额中的逗号转换为double,如果出错,则返回0
*
* @param s 源串,可能为带逗号的金额串;
* @return double
*/
public static Double toDouble(String s) {
return todouble(s);
}

/**
* 自动过滤金额中的逗号转换为double,如果出错,则返回0
*
* @param s 源串,可能为带逗号的金额串;
* @return double
*/
public static double todouble(String s) {
try {
return Double.parseDouble(s.replaceAll(",", ""));
} catch (Exception e) {
return 0.00;
}
}

/**
* 获取double,主要过滤d为null的情况;
*
* @param d Double对象;
* @return double
*/
public static double todouble(Double d) {
if (d == null) return 0.0d;
return d.doubleValue();
}

/**
* 自动过滤金额中的逗号转换为float,如果出错,则返回0
*
* @param s 源串,可能为带逗号的金额串;
* @return Float
*/
public static Float toFloat(String s) {
return tofloat(s);
}

/**
* 自动过滤金额中的逗号转换为float,如果出错,则返回0
*
* @param s 源串,可能为带逗号的金额串;
* @return Float
*/
public static float tofloat(String s) {
try {
return Float.parseFloat(s.replaceAll(",", ""));
} catch (Exception e) {
return 0.0f;
}
}

public static long tolong(String src, long defaultvalue) {
try {
return Long.parseLong(src);
} catch (Exception e) {
return defaultvalue;
}
}

public static int toint(String src, int defaultvalue) {
try {
return Integer.parseInt(src);
} catch (Exception e) {
return defaultvalue;
}
}

/**
* 考虑使用中的精度,判断一个Value是否>0,实际是>0.00001
*
* @param value double类型
* @return boolean
*/
public static boolean isBigThanZero(double value) {
return (value > 0.00001);
}

/**
* 考虑使用中的精度,判断一个Value是否>0,实际是>0.00001
*
* @param value String类型
* @return boolean
*/
public static boolean isBigThanZero(String value) {
return !StringUtil.isEmpty(value) && isBigThanZero(toDouble(value));
}

/**
* 考虑使用中的精度,判断一个Value是否=0,实际是给出一个值范围。
*
* @param value double类型
* @return boolean
*/
public static boolean isEqualsZero(double value) {
return (-0.00001 < value && value < 0.00001);
}

/**
* 考虑使用中的精度,判断一个Value是否=0,实际是给出一个值范围。
*
* @param value String类型
* @return boolean
*/
public static boolean isEqualsZero(String value) {
return StringUtil.isEmpty(value) || isEqualsZero(toDouble(value));
}

/**
* 是否是负数
*
* @param db_val 要判断的double
* @return 负数则返回true;
*/
public static boolean isNegative(double db_val) {
return (compare(db_val, 0.00D) == -1);
}

/**
* 是否是正数
*
* @param db_val 要判断的double
* @return 正数则返回true;
*/
public static boolean isPlus(double db_val) {
return (compare(db_val, 0.00D) == 1);
}

/**
* 得到金额字符串,保持小数点2位
*
* @param db 将double转换为金额字符串;
* @return 金额字符串#0.00;
*/
public static String toStdNumberString(double db) {
try {
return stdNumberFormat.format(db);
} catch (Exception e) {
return "0.00";
}
}

public static String toStdNumberStringEx(double db) {
try {
if (compare(db, -1d) == 0) return "-";
return stdNumberFormat.format(db);
} catch (Exception e) {
return "0.00";
}
}

/**
* 将金额格式字符串,如23,333,093.01 去掉逗号
*
* @param s 金额串
* @return String 去掉逗号后的串,如果amount为空,则返回0.00
*/
public static String toStdNumberString(String s) {
if (StringUtil.isEmpty(s))
return "0.00";
return stdNumberFormat.format(UtilMath.todouble(s));
}

/**
* 将数据转换为两位小数的数字格式;
*
* @param d 数据
* @param isZeroToEmpty 如果未0,是否返回“”;
* @return 两位小数的字符串;
*/
public static String toStdNumberString(double d, boolean isZeroToEmpty) {
if (isEqualsZero(d)) {
return isZeroToEmpty ? "": "0.00";
}
return stdNumberFormat.format(d);
}

public static String toStdNumberString(String s, boolean isZeroToEmpty) {
return toStdNumberString(UtilMath.todouble(s), isZeroToEmpty);
}

public static String toStdNumberString(double d, int scale) {
DecimalFormat dfn = null;
if (scale == 1) dfn = df1Format;
if (scale == 2) dfn = df2Format;
else if (scale == 4) dfn = df4;
else if (scale == 6) dfn = df6NumberF;
else if (scale <= 0) dfn = dfLong;
else {
StringBuilder sb = new StringBuilder("###,###,###,###,##0.");
for (int i = 0; i < scale; i++) sb.append("0");
dfn = new DecimalFormat(sb.toString());
}
return dfn.format(d);
}

/**
* 将数字乘100,保留小数点后两位, 然后后面添加%
*
* @param d 值
* @param isZeroToEmpty,如果值为0,是否返回空;
* @return 字符串;
*/
public static String toStdPercentNumberStr(double d, boolean isZeroToEmpty) {
if (d > -0.00000000001 && d < 0.00000000001) {
return isZeroToEmpty ? "": "0.00%";
}
return toStdNumberString(d * 100) + "%";
}


public static String toStdAmountString(double d) {
return toStdAmountString(d, false);
}

/**
* 将数据转换为两位小数的金额格式,带逗号;
*
* @param d 数据
* @param isZeroToEmpty 如果未0,是否返回“”;
* @return 金额格式的字符串;
*/
public static String toStdAmountString(double d, boolean isZeroToEmpty) {
if (isEqualsZero(d)) {
return isZeroToEmpty ? "": "0.00";
}
return stdAmountFormat.format(d);
}

public static String toStdAmountString(String s) {
return toStdAmountString(UtilMath.todouble(s), false);
}

public static String toStdAmountString(String s, boolean isZeroToEmpty) {
return toStdAmountString(UtilMath.todouble(s), isZeroToEmpty);
}


/**
* 将小写金额转换为人民币大写金额
*
* @param s 金额格式的串
* @return String 转换结果
*/
public static String toCapsAmountString(String s) {
if (StringUtil.isEmpty(s)) return "";
return toCapsAmountString(todouble(s));
}

/**
* 将小写金额转换为人民币大写金额
*
* @param v double
* @return String 转换结果
*/
public static String toCapsAmountString(double v) {
if (v < MIN_VALUE || v > MAX_VALUE) return "参数非法!";

boolean negative = isNegative(v);

if (negative) v = Math.abs(v);
long l = Math.round(v * 100);
if (l == 0) return "零元整";

String strValue = String.valueOf(l);
// i用来控制数
int i = 0;
// j用来控制单位
int j = UNIT.length() - strValue.length();
StringBuilder rs = new StringBuilder(32);
boolean isZero = false;
for (; i < strValue.length(); i++, j++) {
char ch = strValue.charAt(i);
if (ch == '0') {
isZero = true;
if (UNIT.charAt(j) == '亿' || UNIT.charAt(j) == '万' || UNIT.charAt(j) == '元') {
rs.append(UNIT.charAt(j));
isZero = false;
}
} else {
if (isZero) {
rs.append('零');
isZero = false;
}
rs.append(DIGIT.charAt(ch - '0')).append(UNIT.charAt(j));
}
}
if (rs.charAt(rs.length() - 1) != '分')
rs.append('整');

i = rs.indexOf("亿万");
if (i > 0) rs.delete(i + 1, i + 2); // i+1 ->万

if (negative)
return rs.insert(0, '负').toString();
else
return rs.toString();
}

/**
* 返回0 到 maxvalue的随机数
*
* @param maxvalue 随机数的最大值
* @return int
*/
public static int rnd(int maxvalue) {
return (int) (Math.random() * (maxvalue + 1));
}


public static double chkDbNull(Double v) {
return v == null ? 0: v;
}

public static double max(double d1, double d2) {
return compare(d1, d2) < 0 ? d2 : d1;
}

public static double min(double d1, double d2) {
return compare(d1, d2) < 0 ? d1 : d2;
}


public static void main(String[] args) {
double aa=123456789.345678900005;
System.out.println("args0 = " + upDouble(aa,0));
System.out.println("args1 = " + upDouble(aa,1));
System.out.println("args2 = " + upDouble(aa,2));
System.out.println("args3 = " + upDouble(aa,3));
System.out.println("args4 = " + upDouble(aa,4));
System.out.println("args5 = " + upDouble(aa,5));
System.out.println("args5 = " + upDouble(aa,6));
System.out.println("args5 = " + upDouble(aa,7));
System.out.println("args5 = " + upDouble(aa,8));

}

/**
* double 去尾法
* @param src 待处理数据
* @param scale 保留小数位数
* @return
*/
public static double cutDouble(double src, int scale){
String v = toStdNumberString(src, 6);//先到6位小数,再去计算,否则容易出错,如8.3成8.29999999999,舍位就成了8.29了
DecimalFormat formater = new DecimalFormat();
formater.setMaximumFractionDigits(scale);
formater.setGroupingSize(0);
formater.setRoundingMode(RoundingMode.FLOOR);
return new BigDecimal(formater.format(toDouble(v))).doubleValue();
}

/**
* double 进位法
* @param src 待处理数据
* @param scale 保留小数位数
* @return
*/
public static double upDouble(double src, int scale){
String v = toStdNumberString(src, 6);//先到6位小数,再去计算
DecimalFormat formater = new DecimalFormat();
formater.setMaximumFractionDigits(scale);
formater.setGroupingSize(0);
formater.setRoundingMode(RoundingMode.UP);
return new BigDecimal(formater.format(toDouble(v))).doubleValue();
}
}

+ 0
- 506
smtweb-framework/bpm/src/main/java/cc/smtweb/system/bpm/util/UtilString.java Ver arquivo

@@ -1,506 +0,0 @@
package cc.smtweb.system.bpm.util;


import cc.smtweb.framework.core.util.NumberUtil;
import cc.smtweb.framework.core.util.StringUtil;

import java.text.Collator;
import java.util.*;

/**
* Created with IntelliJ IDEA.
* User: AKhh
* Date: 12-12-24 下午5:09
* To change this template use File | Settings | File Templates.
*/
@SuppressWarnings("UnusedDeclaration")
@Deprecated
public class UtilString {
private static Collator chineseCollator = Collator.getInstance(Locale.CHINA);

/**
* strSrc中寻找第一个strSe并且返回以其分隔的Left部分,汉字长度也为1
*
* @param strSrc 源字符串
* @param strSe 分割字符
* @return String 返回
*/
public static String getLeft(String strSrc, String strSe) {
if (StringUtil.isEmpty(strSrc))
return "";
if (StringUtil.isEmpty(strSe))
strSe = " ";

String result = "";
int pos = strSrc.indexOf(strSe);
if (pos >= 0)
result = strSrc.substring(0, pos);
return result;
}

/**
* 返回字符串的左边部分,汉字长度也为1
*
* @param strSrc 源串,如果为空,则返回“”;
* @param count 要获取的右边字符串长度,负数将返回“”,如果count>字符串长度,则返回整个字符串;
* @return String return
*/
public static String getLeft(String strSrc, int count) {
if (StringUtil.isEmpty(strSrc) || count <= 0) {
return "";
}
if (strSrc.length() < count) {
return strSrc;
} else {
return strSrc.substring(0, count);
}
}

/**
* strSrc中寻找第一个strSe并且返回以其分隔的Right部分,汉字长度也为1
*
* @param strSrc 源串
* @param strSe 分隔符,一个字符
* @return String right部分
*/
public static String getRight(String strSrc, String strSe) {
if (StringUtil.isEmpty(strSrc))
return "";
if (StringUtil.isEmpty(strSe))
strSe = " ";

String result = strSrc;
int pos = strSrc.indexOf(strSe);
if (pos >= 0)
result = strSrc.substring(pos + strSe.length());
return result;
}

/**
* 返回字符串的右边部分,汉字长度也为1
*
* @param strSrc 源串
* @param count 要获取的右边字符串长度,负数将返回“”,如果count>字符串长度,则返回整个字符串;
* @return String return
*/
public static String getRight(String strSrc, int count) {
if (StringUtil.isEmpty(strSrc) || count <= 0) {
return "";
}
int l = strSrc.length();
if (l <= count) {
return strSrc;
}
return strSrc.substring(l - count);
}

/**
* 左边补齐字符
*
* @param src 源串
* @param pad 补齐字符
* @param length 最终长度
* @return 补齐后的字符串
*/
public static String padLeft(String src, String pad, int length) {
StringBuilder sb = new StringBuilder(repeatString(pad, length));
sb.append(src);
return sb.substring(sb.length() - length);
}

public static String padLeft(long src, String pad, int length) {
StringBuilder sb = new StringBuilder(repeatString(pad, length));
sb.append(src);
return sb.substring(sb.length() - length);
}

public static String padRight(String src, String pad, int length) {
StringBuilder sb = new StringBuilder(length * pad.length() + src.length());
sb.append(src).append(repeatString(pad, length));
return sb.substring(0, length);
}

public static String padRight(long src, String pad, int length) {
StringBuilder sb = new StringBuilder(length * pad.length());
sb.append(src).append(repeatString(pad, length));
return sb.substring(0, length);
}

/**
* 由于jdk1.3提供的replace函数不能满足替换要求,自己写一个
*
* @param src 源串
* @param oldS 将...
* @param newS 替换成...
* @return 替换后的字符串
*/
public static String replaceString(String src, String oldS, String newS) {
StringBuilder ret = new StringBuilder(64);
int pos = src.indexOf(oldS);
while (pos >= 0) {
ret.append(src, 0, pos).append(newS);
src = src.substring(pos + oldS.length());
pos = src.indexOf(oldS);
}
ret.append(src);
return ret.toString();
}

/**
* 取得指定字符串左边的有效数字,首先去掉两边空格
*
* @param s 源串
* @return 串左边的有效数字
*/
public static String getStringLeftNumber(String s) {
String ret = "";
int dotCount = 0;
s = s.trim();
char[] carr = s.toCharArray();
for (char aCarr : carr) {
if (Character.isDigit(aCarr)) {
ret += aCarr;
} else if (aCarr == '.' && dotCount == 0) {
ret += aCarr;
dotCount++;
} else {
break;
}
}
if (ret.endsWith(".")) {
ret = ret.substring(0, ret.length() - 1);
}
return ret;
}

/**
* 取得重复字串
*
* @param repeatString 重复字串
* @param count 重复次数
* @return String
*/
public static String repeatString(String repeatString, int count) {
if (count <= 0) return "";
StringBuilder ret = new StringBuilder(repeatString.length() * count);
for (int i = 1; i <= count; i++) {
ret.append(repeatString);
}
return ret.toString();
}

/**
* 去除字符串左边的指定字符串
*
* @param src 源字符串
* @param cut 要去掉的字符串;
* @return 处理结果
*/
public static String cutStringLeft(String src, String cut) {
if (StringUtil.isEmpty(src) || StringUtil.isEmpty(cut)) {
return "";
}
if (src.startsWith(cut)) {
return cutStringLeft(src.substring(cut.length()), cut);
} else {
return src;
}
}

public static String cutStringRight(String src, String cut) {
if (StringUtil.isEmpty(src) || StringUtil.isEmpty(cut)) {
return "";
}
while (src.endsWith(cut))
src = src.substring(0, src.length() - cut.length());

return src;
}

/**
* Removes all spaces from a string
* 可以替换大部分空白字符, 不限于空格,\s 可以匹配空格、制表符、换页符等空白字符的其中任意一个
*/
public static String removeSpaces(String str) {
return str.replaceAll("\\s*", "");
}

/**
* Creates a single string from a List of strings seperated by a delimiter.
*
* @param list a list of strings to join
* @param delim the delimiter character(s) to use. (null value will join with no delimiter)
* @return a String of all values in the list seperated by the delimiter
*/
public static String join(List<String> list, String delim) {
if (list == null || list.size() < 1)
return null;
StringBuffer buf = new StringBuffer();
Iterator i = list.iterator();

while (i.hasNext()) {
buf.append((String) i.next());
if (i.hasNext())
buf.append(delim);
}
return buf.toString();
}

/**
* Splits a String on a delimiter into a List of Strings.
*
* @param str the String to split
* @param delim the delimiter character(s) to join on (null will split on whitespace)
* @return a list of Strings
*/
public static List<String> split(String str, String delim) {
List<String> splitList = new ArrayList<>();
StringTokenizer st;
if (str == null)
return splitList;

if (delim != null)
st = new StringTokenizer(str, delim);
else
st = new StringTokenizer(str);

while (st.hasMoreTokens()) {
splitList.add(st.nextToken());
}
return splitList;
}

//是否为true,(1,y,true,yes)
public static boolean toBoolean(String v) {
return "1".equals(v) || "y".equalsIgnoreCase(v) || "true".equalsIgnoreCase(v) || "yes".equalsIgnoreCase(v);
}

public static int chineseCompare(String s1, String s2) {
return chineseCollator.compare(s1, s2);
}

/**
* 按照编码级次,得到类型的真实编码,主要用于like 'parentCode%'
* getSplitTypeCode('GF82000',2, 2, 1) == GF82
* getSplitTypeCode('GF82100',3, 2, 1) == GF821
* getSplitTypeCode('82100' ,3, 0, 1) == 821
*
* @param curLevel 当前编码的所在层次
* @param startIndex 数字编码的开始选项
* @param perSize 每层的数字编码长度
*/
public static String getRealCode(String code, int curLevel, int startIndex, int perSize) {
StringBuilder sb = new StringBuilder(code.length());
if (startIndex > 0) sb.append(code, 0, startIndex);
for (int i = startIndex, l = 0; i < code.length(); i += perSize) {
if (l < curLevel) {
sb.append(code, i, i + perSize);
++l;
} else {
break;
}
}
return sb.toString();
}

//函数功能: 正整数
public static boolean isPureNumber(String inputString) {
return inputString.matches("^[1-9]\\d*$");
}

//函数功能: 整数
public static boolean isNumber(String inputString) {
return inputString.matches("^[-+][0-9]\\d*$");
}

//函数功能: 浮点数
public static boolean isAmount(String inputString) {
return inputString.matches("^[-+]?[\\d,]+(\\.\\d+)?$");
}

//函数功能: 带千分号的整数
public static boolean isFormatNumber(String inputString) {
return inputString.matches("^[-+]?[\\d,]+(\\d+)?$");
}


//首字母大写
public static String upFirst(String s) {
return s.substring(0, 1).toUpperCase() + s.substring(1);
}

public static String padRightBytes(String src, String pad, int length) {
length -= src.replaceAll("[^\\x00-\\xff]", "**").length();
return src + repeatString(pad, length);
}

//按字节数取子串,begin不是按字节的
public static String substrByte(String src, int begin, int len) {
StringBuilder sb = new StringBuilder(32);
char c;
int tl = src.length();
for (int i = begin; i < len + begin && i < tl; i++) {

c = src.charAt(i);
sb.append(c);
if (String.valueOf(c).replaceAll("[^\\x00-\\xff]", "**").length() > 1) {
// 遇到中文汉字,截取字节总数减1
--len;
}
}
return sb.toString();
}

/**
* 通配符算法。 可以匹配"*"和"?"
* 如a*b?d可以匹配aAAAbcd
*
* @param pattern 匹配表达式
* @param str 匹配的字符串
* @return
*/
public static boolean match(String pattern, String str) {
if (pattern == null || str == null)
return false;

boolean result = false;
char c; // 当前要匹配的字符串
boolean beforeStar = false; // 是否遇到通配符*
int back_i = 0;// 回溯,当遇到通配符时,匹配不成功则回溯
int back_j = 0;
int i, j;
for (i = 0, j = 0; i < str.length(); ) {
if (pattern.length() <= j) {
if (back_i != 0) {// 有通配符,但是匹配未成功,回溯
beforeStar = true;
i = back_i;
j = back_j;
back_i = 0;
back_j = 0;
continue;
}
break;
}

if ((c = pattern.charAt(j)) == '*') {
if (j == pattern.length() - 1) {// 通配符已经在末尾,返回true
result = true;
break;
}
beforeStar = true;
j++;
continue;
}

if (beforeStar) {
if (str.charAt(i) == c) {
beforeStar = false;
back_i = i + 1;
back_j = j;
j++;
}
} else {
if (c != '?' && c != str.charAt(i)) {
result = false;
if (back_i != 0) {// 有通配符,但是匹配未成功,回溯
beforeStar = true;
i = back_i;
j = back_j;
back_i = 0;
back_j = 0;
continue;
}
break;
}
j++;
}
i++;
}

if (i == str.length() && j == pattern.length())// 全部遍历完毕
result = true;
return result;
}

//填充变量
public static String myReplaceStrEx(String express, String b, String e, IStrHanlder hanlder) {
if (null == express) return express;
int keyBegin = 0, keyEnd = 0;
int lb = b.length(), le = e.length();
String fn;
while (true) {
keyBegin = express.indexOf(b, keyBegin);
if (keyBegin < 0) break;
keyEnd = express.indexOf(e, keyBegin);
if (keyEnd <= keyBegin) break;
keyBegin++;
fn = express.substring(keyBegin + lb - 1, keyEnd);
fn = hanlder.work(fn);
if (fn != null) {
express = express.substring(0, keyBegin - 1) + fn + express.substring(keyEnd + le);
}
}
return express;
}

//填充变量
public static String myReplaceStrEx(String express, String b, String e, final Map<String, String> mapVals) {
return myReplaceStrEx(express, b, e, new IStrHanlder() {
@Override
public String work(String src) {
return StringUtil.checkNull(mapVals.get(src));
}
});
}

/*Blob转String*/
// public static String blob2Str(Blob blob) {
// if (blob == null) return "";
// ByteArrayOutputStream outStream = null;
// try {
// long len = blob.length();
// if (len == 0L) return "";
// byte[] bytes;
// long i = 1L;
// outStream = new ByteArrayOutputStream();
// while (i < len) {
// bytes = blob.getBytes(i, 1024);
// i += 1024L;
// outStream.write(bytes);
// }
//
// return UtilEncode.base64EncodeB(outStream.toByteArray());
// } catch (Exception e) {
// e.printStackTrace();
// return "";
// } finally {
// IOUtils.closeQuietly(outStream);
// }
// }


public interface IStrHanlder {
String work(String src);
}

public static int[] splitStr(String src, String ch) {
String[] ss = src.split(ch);
int[] ret = new int[ss.length];
for (int i = 0, len = ss.length; i < len; i++) {
ret[i] = NumberUtil.getIntIgnoreErr(ss[i]);
}
return ret;
}

public static void main(String[] args) {
String s = "a[#[#123#]bcde[aaa]bcd";
s = UtilString.myReplaceStrEx(s, "[#", "#]", new UtilString.IStrHanlder() {
@Override
public String work(String src) {
if (src.equals("123")) return "1";
return null;
}
});
System.out.println(s);
}
}

Carregando…
Cancelar
Salvar