package com.utils;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.MessageDigest;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 各种数据类型转换成字符串类型工具类
* @author 尹俊
* @date 2016/03/17
*/
public class StringTools {
private static DecimalFormat formater = new DecimalFormat();
/**
* 整数常量
* */
public static final String DEC_INT = "#";
/**
* FLOAT常量
* */
public static final String DEC_FLOAT = "#0.00";
/**
* DOUBLE常量
* */
public static final String DEC_DOUBLE = "#0.000";
/**
* 金额常量
* */
public static final String DEC_MONEY = "###,##0.00";
/**
*
* @param l 入参需要转换的数据
* @param format 格式比如 #0.00
* @return
*/
public static String getDecimalString(long l, String format) {
if (!format.equals(formater.toPattern())) {
formater.applyPattern(format);
}
return formater.format(l);
}
/**
*
* @param d 入参需要转换的数据
* @param format 格式比如 #0.00
* @return
*/
public static String getDecimalString(double d, String format) {
if (!format.equals(formater.toPattern())) {
formater.applyPattern(format);
}
return formater.format(d);
}
/**
*
* @param s 入参需要转换的数据
* @param format 格式比如 #0.00
* @return
*/
public static Number parseNumber(String s, String format) {
if (!format.equals(formater.toPattern())) {
formater.applyPattern(format);
}
try {
return formater.parse(s);
} catch (Exception e) {
return null;
}
}
/**
*
* @param s
* @param code
* @return
*/
public static String URLEncoder(String s, String code) {
s = s.replace('\\', '/');
String[] split = s.split("/");
StringBuffer encode = new StringBuffer();
try {
for (int i = 0; i < split.length; i++) {
encode.append("/").append(
java.net.URLEncoder.encode(split[i], code));
}
} catch (java.io.UnsupportedEncodingException ex) {
return null;
}
return encode.substring(1);
}
/**
*
* @param s
* @param code
* @return
*/
public static String URLDecoder(String s, String code) {
s = s.replace('\\', '/');
String[] split = s.split("/");
StringBuffer decode = new StringBuffer();
try {
for (int i = 0; i < split.length; i++) {
decode.append("/").append(
java.net.URLDecoder.decode(split[i], code));
}
} catch (java.io.UnsupportedEncodingException ex) {
return null;
}
return decode.substring(1);
}
/**
*
* @param password
* @param algorithm
* Message Digest Algorithms The algorithm names in this section
* can be specified when generating an instance of MessageDigest.
* MD2: The MD2 message digest algorithm as defined in RFC 1319.
* MD5: The MD5 message digest algorithm as defined in RFC 1321.
* SHA-1: The Secure Hash Algorithm, as defined in Secure Hash
* Standard, NIST FIPS 180-1. SHA-256, SHA-384, and SHA-512: New
* hash algorithms for which the draft Federal Information
* Processing Standard 180-2, Secure Hash Standard (SHS) is now
* available. SHA-256 is a 256-bit hash function intended to
* provide 128 bits of security against collision attacks, while
* SHA-512 is a 512-bit hash function intended to provide 256
* bits of security. A 384-bit hash may be obtained by truncating
* the SHA-512 output.
* @return
*/
public static String encodePassword(String password, String algorithm) {
byte[] unencodedPassword = password.getBytes();
MessageDigest md = null;
try {
// first create an instance, given the provider
md = MessageDigest.getInstance(algorithm);
} catch (Exception e) {
return password;
}
md.reset();
// call the update method one or more times
// (useful when you don't know the size of your data, eg. stream)
md.update(unencodedPassword);
// now calculate the hash
byte[] encodedPassword = md.digest();
StringBuffer buf = new StringBuffer();
for (int i = 0; i < encodedPassword.length; i++) {
if ((encodedPassword[i] & 0xff) < 0x10) {
buf.append("0");
}
buf.append(Long.toString(encodedPassword[i] & 0xff, 16));
}
return buf.toString();
}
/**
*
* @param str
* @return
*/
public static String encodeString(String str) {
sun.misc.BASE64Encoder encoder = new sun.misc.BASE64Encoder();
return encoder.encodeBuffer(str.getBytes()).trim();
}
/**
*
* @param str
* @return
*/
public static String decodeString(String str) {
sun.misc.BASE64Decoder dec = new sun.misc.BASE64Decoder();
try {
return new String(dec.decodeBuffer(str));
} catch (IOException io) {
throw new RuntimeException(io.getMessage(), io.getCause());
}
}
/**
* 判断字符串是否为空或null
*
* @param str
* @return
*/
public static boolean isBlank(String str) {
int strLen;
if (str == null || (strLen = str.length()) == 0) {
return true;
}
for (int i = 0; i < strLen; i++) {
if (!Character.isWhitespace(str.charAt(i))) {
return false;
}
}
return true;
}
/**
*
* @param string
* @param encoding
* @return
*/
public static String toEncoding(String string, String encoding) {
if (string == null) {
return null;
}
try {
byte[] bytes = string.getBytes("ISO8859-1");
return new String(bytes, encoding);
} catch (java.io.UnsupportedEncodingException ex) {
return null;
}
}
/**
*
* @param string
* @param oldEncoding
* @param newEncoding
* @return
*/
public static String toEncoding(String string, String oldEncoding,
String newEncoding) {
if (string == null) {
return null;
}
try {
byte[] bytes = string.getBytes(oldEncoding);
return new String(bytes, newEncoding);
} catch (java.io.UnsupportedEncodingException ex) {
return null;
}
}
/**
* 判断字符串能否转整型数字
* @param value需要判断的字符串
* @return
*/
public static boolean isInt(String value) {
try {
Integer.parseInt(value);
return true;
} catch (Exception ex) {
return false;
}
}
/**
* 判断字符是否能否转LONG型数字
* @param value 需要判断的字符串
* @return
*/
public static boolean isLong(String value) {
try {
Long.parseLong(value);
return true;
} catch (Exception ex) {
return false;
}
}
/**
* 字符串转金额字符串
* @param s
* @return
*/
public static String getMoneyString(String s) {
if (s == null || s.equals("")) {
return "0.00";
}
try {
return getMoneyString(Double.parseDouble(s));
} catch (Exception e) {
return s;
}
}
/**
* DOUBLE转金额字符串
* @param d
* @return
*/
public static String getMoneyString(double d) {
if (!DEC_MONEY.equals(formater.toPattern())) {
formater.applyPattern(DEC_MONEY);
}
return formater.format(d);
}
/**
* 字符串转DOUBLE金额
* @param s
* @return
*/
public static double parseMoney(String s) {
if (!DEC_MONEY.equals(formater.toPattern())) {
formater.applyPattern(DEC_MONEY);
}
try {
return formater.parse(s).doubleValue();
} catch (Exception e) {
return 0;
}
}
/**
*
* @param s
* @return
*/
public static String getDoubleString(String s) {
if (s == null || s.equals("")) {
return "0.000";
}
try {
return getDoubleString(Double.parseDouble(s));
} catch (Exception e) {
return