package javax.core.common.utils;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.regex.Pattern;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
/**
* String工具类
*
* @author Tom
*/
public class StringUtils {
private StringUtils(){}
/**
* 是否为空 是则返回一个空字符
*
* @param str
* @return String
*/
public static String notNull(String str) {
return str == null ? "" : str;
}
/**
* 格式化字符
*
* @param str
* @param args
* @return String
*/
public static String format(String str, Object... args) {
String result = str;
java.util.regex.Pattern p = java.util.regex.Pattern
.compile("\\{(\\d+)\\}");
java.util.regex.Matcher m = p.matcher(str);
while (m.find()) {
int index = Integer.parseInt(m.group(1));
if (index < args.length) {
result = result.replace(m.group(),
ObjectUtils.notNull(args[index], "").toString());
}
}
return result;
}
/**
* 编码
*
* @param str
* @return
*/
public static String coding(String str) {
return coding(str, "ISO-8859-1");
}
public static String coding(String str, String charset) {
return coding(str, charset, "UTF-8");
}
public static String coding(String str, String charset, String tocharset) {
try {
return str == null ? "" : new String(str.getBytes(charset),
tocharset);
} catch (Exception E) {
return str;
}
}
/**
* Escape 编码
*
* @param src
* @return
*/
public static String escape(String src) {
int i;
char j;
StringBuffer tmp = new StringBuffer();
tmp.ensureCapacity(src.length() * 6);
for (i = 0; i < src.length(); i++) {
j = src.charAt(i);
if (Character.isDigit(j) || Character.isLowerCase(j)
|| Character.isUpperCase(j))
tmp.append(j);
else if (j < 256) {
tmp.append("%");
if (j < 16)
tmp.append("0");
tmp.append(Integer.toString(j, 16));
} else {
tmp.append("%u");
tmp.append(Integer.toString(j, 16));
}
}
return tmp.toString();
}
/**
* 截取字符串
*
* @param src
* @param start
* @param length
* @param ov
* @return
*/
public static String substring(String src, int start, int length, String ov) {
if (src != null && length(src) > length) {
try {
byte[] rc = src.getBytes("GBK");
short charlen = 2;
int count = 0;
if (!new String(rc).equals(src)) {
rc = src.getBytes("UTF-8");
charlen = 3;
}
length = Math.max(Math.min(length - start, rc.length), 1);
byte[] bs = new byte[length];
System.arraycopy(rc, start, bs, 0, length);
for (byte c : bs)
if (c < 0)
count++;
if (count % charlen != 0)
return substring(src, start, length - (count % charlen), ov);
return new String(bs) + ov;
} catch (Exception e) {
return src.substring(start, length - ov.length()) + ov;
}
}
return src;
}
/**
* bytes to hex string
*
* @param bytes
* @return
*/
public static String bytes2Hex(byte[] bytes) {
StringBuilder sb = new StringBuilder();
for (byte b : bytes) {
int i = b & 0xFF;
if (i <= 0xF) {
sb.append("0");
}
sb.append(Integer.toHexString(i));
}
return sb.toString();
}
// public static final List<Character> HEX_CHAR_LIST;
// static {
// HEX_CHAR_LIST = new ArrayList<Character>();
// HEX_CHAR_LIST.add(new Character('0'));
// HEX_CHAR_LIST.add(new Character('1'));
// HEX_CHAR_LIST.add(new Character('2'));
// HEX_CHAR_LIST.add(new Character('3'));
// HEX_CHAR_LIST.add(new Character('4'));
// HEX_CHAR_LIST.add(new Character('5'));
// HEX_CHAR_LIST.add(new Character('6'));
// HEX_CHAR_LIST.add(new Character('7'));
// HEX_CHAR_LIST.add(new Character('8'));
// HEX_CHAR_LIST.add(new Character('9'));
// HEX_CHAR_LIST.add(new Character('a'));
// HEX_CHAR_LIST.add(new Character('b'));
// HEX_CHAR_LIST.add(new Character('c'));
// HEX_CHAR_LIST.add(new Character('d'));
// HEX_CHAR_LIST.add(new Character('e'));
// HEX_CHAR_LIST.add(new Character('f'));
// }
// private static byte hex2Byte(String s) {
// int high = HEX_CHAR_LIST.indexOf(new Character(s.charAt(0))) << 4;
// int low = HEX_CHAR_LIST.indexOf(new Character(s.charAt(1)));
//
// return (byte) (high + low);
// }
/**
* hex string to bytes
*
* @param bytes
* @return
*/
public static byte[] hex2Bytes(String input) {
byte[] res = new byte[input.length() / 2];
char[] chs = input.toCharArray();
for(int i = 0,c = 0; i < chs.length; i += 2,c ++){
res[c] = (byte) (Integer.parseInt(new String(chs,i,2), 16));
}
return res;
}
public static String getPrefix(String content, String regex) {
int _index = content.indexOf(regex);
if (_index >= 0) {
return content.substring(0, _index);
}
return content;
}
/**
* 将指定位置的字符转换为小写
*
* @param str
* @param beginIndex
* @param endIndex
* @return
*/
public static String lowerCase(String str, int beginIndex, int endIndex) {
StringBuilder builder = new StringBuilder();
builder.append(str.substring(0, beginIndex));
builder.append(str.substring(beginIndex, endIndex).toLowerCase());
builder.append(str.substring(endIndex));
return builder.toString();
}
/**
* 将指定位置的字符转换为大写
*
* @param str
* @param beginIndex
* @param endIndex
* @return
*/
public static String upperCase(String str, int beginIndex, int endIndex) {
StringBuilder builder = new StringBuilder();
builder.append(str.substring(0, beginIndex));
builder.append(str.substring(beginIndex, endIndex).toUpperCase());
builder.append(str.substring(endIndex));
return builder.toString();
}
/**
* 将首字母转换为小写
*
* @param iString
* @return
*/
public static String lowerCaseFirstChar(String iString) {
StringBuilder builder = new StringBuilder();
builder.append(iString.substring(0, 1).toLowerCase());
builder.append(iString.substring(1));
return builder.toString();
}
/**
* 将首字母转换为大写
*
* @param iString
* @return
*/
public static String upperCaseFirstChar(String iString) {
StringBuilder builder = new StringBuilder();
builder.append(iString.substring(0, 1).toUpperCase());
builder.append(iString.substring(1));
return builder.toString();
}
/**
* 检查子符串出现过几次
*
* @param str
* 源字符串
* @param subStr
* 子字符串
* @return
*/
public static int timesOf(String str, String subStr) {
int foundCount = 0;
if (subStr.equals("")) {
return 0;
}
int fromIndex = str.indexOf(subStr);
while (fromIndex != -1) {
++foundCount;
fromIndex = str.indexOf(subStr, fromIndex + subStr.length());
}
return foundCount;
}
/**
* 去除两边空格(包括全角空格), null 值以空字符代替
*
* @param s
* @return
*/
public static String trim(String s) {
if (s == null)
return "";
else
return s.trim();
}
/**
* 去除右边的空格
*
* @param str
* @return
*/
public static String rightTrim(String str) {
if (str == null) {
return "";
}
int length = str.length();
for (int i = length - 1; i >= 0; --i) {
if (str.charAt(i) != ' ') {
break;
}
--length;
}
return str.substring(0, length);
}
/**
* 去除左边的空格
*
* @param str
* @return
*/
public static String leftTrim(String str) {
if (str == null) {
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
utils.rar (5个子文件)
utils
DataUtils.java 16KB
StringUtils.java 30KB
BeanUtils.java 10KB
ObjectUtils.java 2KB
GenericsUtils.java 2KB
共 5 条
- 1
资源评论
qq_37128966
- 粉丝: 0
- 资源: 4
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功