272 lines
7.5 KiB
Plaintext
272 lines
7.5 KiB
Plaintext
package com.bonus.core;
|
||
|
||
import java.io.ByteArrayOutputStream;
|
||
import java.text.DecimalFormat;
|
||
import java.util.regex.Matcher;
|
||
import java.util.regex.Pattern;
|
||
|
||
public class StringHelper {
|
||
|
||
private static String hexString = "0123456789ABCDEF";
|
||
|
||
public static String replaceAll(String str, String oldStr, String newStr) {
|
||
return str.replaceAll(oldStr, newStr);
|
||
}
|
||
|
||
public static boolean contains(String s1, String s2) {
|
||
if (isEmpty(s1)) {
|
||
return false;
|
||
}
|
||
return s1.contains(s2);
|
||
}
|
||
|
||
public static int getInt(String valStr) {
|
||
return getInt(valStr, 0);
|
||
}
|
||
|
||
public static int getInt(String valStr, int defaulVal) {
|
||
int val = 0;
|
||
try {
|
||
val = Integer.parseInt(valStr);
|
||
} catch (Exception e) {
|
||
val = defaulVal;
|
||
}
|
||
return val;
|
||
}
|
||
|
||
public static String nullPlaceholder(String str, String holderStr) {
|
||
if (isEmpty(str)) {
|
||
return holderStr;
|
||
}
|
||
return str;
|
||
}
|
||
|
||
public static String getShiroBtns(String str) {
|
||
StringBuilder btnString = new StringBuilder();
|
||
|
||
if (isNotEmpty(str) && str.indexOf("Management") != -1) {
|
||
|
||
Integer startIndex = str.lastIndexOf("/") + 1;
|
||
Integer endIndex = str.indexOf("Management");
|
||
str = str.substring(startIndex, endIndex);
|
||
String startStr1 = "<shiro:hasPermission name='";
|
||
String startStr2 = "'>";
|
||
String endStr = "</shiro:hasPermission>";
|
||
|
||
// 增加按钮
|
||
btnString.append(startStr1 + str + ":add" + startStr2);
|
||
btnString.append(
|
||
"<a title='新增' onclick='add()' class='lrspace3' ><i class='icon-plus-sign color bigger-180'></i></a>");
|
||
btnString.append(endStr);
|
||
// 查看按钮
|
||
btnString.append(startStr1 + str + ":view" + startStr2);
|
||
btnString.append(
|
||
"<a title='查看' onclick='view()' class='lrspace3' ><i class='icon-info-sign color bigger-180'></i></a>");
|
||
btnString.append(endStr);
|
||
// 修改按钮
|
||
btnString.append(startStr1 + str + ":edit" + startStr2);
|
||
btnString.append(
|
||
"<a title='修改' onclick='edit()' class='lrspace3' ><i class='icon-edit-sign color bigger-180'></i></a>");
|
||
btnString.append(endStr);
|
||
// 删除按钮
|
||
btnString.append(startStr1 + str + ":del" + startStr2);
|
||
btnString.append(
|
||
"<a title='删除' onclick='del()' class='lrspace3' ><i class='icon-remove-sign color-red bigger-180'></i></a>");
|
||
btnString.append(endStr);
|
||
}
|
||
|
||
return btnString.toString();
|
||
}
|
||
|
||
public static Integer ceshi(Integer a, Integer b) {
|
||
Integer res = 0;
|
||
res = (int) Math.floor(a / b);
|
||
return res;
|
||
}
|
||
|
||
public static float getFloat(String valStr) {
|
||
return getFloat(valStr, 0);
|
||
}
|
||
|
||
public static float getFloat(String valStr, int defaulVal) {
|
||
float val = 0;
|
||
try {
|
||
val = Float.parseFloat(valStr);
|
||
} catch (Exception e) {
|
||
val = defaulVal;
|
||
}
|
||
return val;
|
||
}
|
||
|
||
/*
|
||
* 将字符串编码成16进制数字,适用于所有字符(包括中文)
|
||
*/
|
||
public static String encode(String str) {
|
||
// 根据默认编码获取字节数组
|
||
byte[] bytes = str.getBytes();
|
||
StringBuilder sb = new StringBuilder(bytes.length * 2);
|
||
// 将字节数组中每个字节拆解成2位16进制整数
|
||
for (int i = 0; i < bytes.length; i++) {
|
||
sb.append(hexString.charAt((bytes[i] & 0xf0) >> 4));
|
||
sb.append(hexString.charAt((bytes[i] & 0x0f) >> 0));
|
||
}
|
||
return sb.toString();
|
||
}
|
||
|
||
public static String decode(String bytes) {
|
||
ByteArrayOutputStream baos = new ByteArrayOutputStream(bytes.length() / 2);
|
||
// 将每2位16进制整数组装成一个字节
|
||
for (int i = 0; i < bytes.length(); i += 2)
|
||
baos.write((hexString.indexOf(bytes.charAt(i)) << 4 | hexString.indexOf(bytes.charAt(i + 1))));
|
||
return new String(baos.toByteArray());
|
||
}
|
||
|
||
public static String fillPrefixZero(int v, int len) {
|
||
String vStr = v + "";
|
||
while (vStr.length() < len) {
|
||
vStr = "0" + vStr;
|
||
}
|
||
return vStr;
|
||
}
|
||
|
||
public static boolean isEmpty(String str) {
|
||
if (str == null || "".equals(str.trim()) || "undefined".equals(str.trim())) {
|
||
return true;
|
||
}
|
||
return false;
|
||
}
|
||
|
||
/**
|
||
* 判断字符串 不为空
|
||
*/
|
||
public static boolean isNotEmpty(String str) {
|
||
return !isEmpty(str);
|
||
}
|
||
|
||
/**
|
||
* 处理时间,获取编号
|
||
*/
|
||
public static String time2Code(String time) {
|
||
if (isNotEmpty(time)) {
|
||
time = time.replace("-", "").replace(" ", "").replace(":", "");
|
||
}
|
||
return time;
|
||
}
|
||
|
||
public static String formatMoneyData(float temp) {
|
||
DecimalFormat df = new DecimalFormat("#0.00");// 构造方法的字符格式这里如果小数不足2位,会以0补足.
|
||
String tmp = df.format(temp);
|
||
return tmp;
|
||
}
|
||
|
||
public static float formatMoney2Data(float temp) {
|
||
String tmp = formatMoneyData(temp);
|
||
float t = getFloat(tmp);
|
||
return t;
|
||
}
|
||
|
||
public static String formatData(float temp) {
|
||
DecimalFormat df = new DecimalFormat("#0.000");// 构造方法的字符格式这里如果小数不足2位,会以0补足.
|
||
String tmp = df.format(temp);
|
||
return tmp;
|
||
}
|
||
|
||
public static float format2Data(float temp) {
|
||
String tmp = formatData(temp);
|
||
float t = getFloat(tmp);
|
||
return t;
|
||
}
|
||
|
||
/**
|
||
* 检查是否存在数据飘逸
|
||
*/
|
||
public static float disposeDrift(Float num) {
|
||
String strNum = num.toString();
|
||
String endStr = strNum.substring(strNum.indexOf(".") + 1);
|
||
String startStr = strNum.substring(0, strNum.indexOf("."));
|
||
if (endStr.indexOf("999") >= 0) {
|
||
int start = endStr.indexOf("999");
|
||
if (start == 0) {
|
||
num = Float.parseFloat(startStr) + 1f;
|
||
} else {
|
||
String a = "0";
|
||
String c = "";
|
||
while (endStr.startsWith(a)) {
|
||
a += "0";
|
||
c += "0";
|
||
}
|
||
endStr = endStr.substring(0, start);
|
||
endStr = c + (Integer.parseInt(endStr) + 1) + "";
|
||
num = Float.parseFloat(startStr + "." + endStr);
|
||
}
|
||
} else if (endStr.indexOf("000") >= 0) {
|
||
int start = endStr.indexOf("000");
|
||
if (start == 0) {
|
||
num = Float.parseFloat(startStr);
|
||
} else {
|
||
num = Float.parseFloat(strNum.substring(0, strNum.indexOf("000")));
|
||
}
|
||
}
|
||
return num;
|
||
}
|
||
|
||
/**
|
||
* java判断字符是否是全部由数字,小写字母,大写字母,特殊符号组成 正则表达式判断
|
||
*
|
||
* @param arr
|
||
*/
|
||
public static void judgeA(String arr) {
|
||
// TODO Auto-generated method stub
|
||
String str1 = "abcdfjijgiaj";
|
||
String str2 = "0AOGVNDIJEIO";
|
||
String str3 = "12345465870";
|
||
String str4 = "@#$^^%^&*(";
|
||
|
||
Pattern pattern1 = Pattern.compile("[a-z]*");
|
||
Pattern pattern2 = Pattern.compile("[A-Z]*");
|
||
Pattern pattern3 = Pattern.compile("[0-9]*");
|
||
Pattern pattern4 = Pattern.compile("\\p{Punct}+");
|
||
|
||
Matcher matcher1 = pattern1.matcher(str1);
|
||
Matcher matcher2 = pattern2.matcher(str2);
|
||
Matcher matcher3 = pattern3.matcher(str3);
|
||
Matcher matcher4 = pattern4.matcher(str4);
|
||
|
||
if (matcher1.matches()) {
|
||
System.out.println("全是小写字母");
|
||
} else {
|
||
System.out.println("不全是小写字母");
|
||
}
|
||
|
||
if (matcher2.matches()) {
|
||
System.out.println("全是大写字母");
|
||
} else {
|
||
System.out.println("不全是大写字母");
|
||
}
|
||
|
||
if (matcher3.matches()) {
|
||
System.out.println("全是数字");
|
||
} else {
|
||
System.out.println("不全是数字");
|
||
}
|
||
|
||
if (matcher4.matches()) {
|
||
System.out.println("全是特殊符号");
|
||
} else {
|
||
System.out.println("不全是特殊符号");
|
||
}
|
||
|
||
// 判断字符串是否全部由数字组成,java自带方法判断
|
||
boolean flag = true;
|
||
for (int i = str3.length() - 1; i >= 0; i--) {
|
||
if (!Character.isDigit(str3.charAt(i))) {
|
||
flag = false;
|
||
}
|
||
}
|
||
if (flag) {
|
||
System.out.println(str3 + "全部是数字");
|
||
}
|
||
|
||
}
|
||
}
|