Java字符串操作工具类(下)

代码内容功能实现:集合判空,判断字符串是否为数字、整形、long,按顺序插入元素,返回字符串长度,转换字节,是否为包装类等。

package com.xiu.util;

import java.text.DecimalFormat;
import java.util.Collection;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *字符串操作工具类
 *
 * @author xiu
 * @version 2017年8月9日 上午10:45:35 
 */
public class StringUtil {

	
	/**
	 * 判断一个集合是否为null或者没有元素
	 * @param collection
	 * @return
	 */
	public static <T> boolean isCollectionEmpty( Collection<T> collection ) {
		if (collection == null || collection.isEmpty()) {
			return true;
		}
		return false;
	}
	
	/**
	 * 判断字符串是否为空
	 * @param str
	 * @return
	 */
	public static boolean isNull(String str) {
		return str == null || str.length() == 0 || str.equals(" ") 
				|| str.equals("null") || str.trim().length() == 0 ;
	}
	
	/**
	 * 是否整形类型
	 * 
	 * @param data
	 * @return
	 */
	public static boolean isInteger(Object data) {
		if (data == null || "".equals(data))
			return false;
		String reg = "[\\d]+";
		Pattern p = Pattern.compile(reg);
		Matcher m = p.matcher(data.toString());
		return Integer.MAX_VALUE >= Double.parseDouble(data.toString())
				&& m.matches();
	}
	
	/**
	 * 功能:判断字符串是否为数字
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isNumeric(String str) {
		Pattern pattern = Pattern.compile("[0-9]*");
		Matcher isNum = pattern.matcher(str);
		if (isNum.matches()) {
			return true;
		} else {
			return false;
		}
	}
	
	/**
	 * 判断字符串是否为long类型
	 * @param obj
	 * @return
	 */
	public static long parseLong(Object obj) {
		if (obj == null || obj.equals("")) {
			return 0;
		}
		try {
			if (!obj.toString().contains(".")) {
				return Long.parseLong(obj.toString());
			}
			return (long) Double.parseDouble((obj.toString()));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 0;
	}
	
	/**
	 * 按顺序加入元素(插入排序)
	 * 
	 * @param list
	 * @param o
	 *            (实现Comparable接口)
	 * @param true-降序 false-升序
	 */
	@SuppressWarnings("unchecked")
	public static <T> void addOrderList(List<T> list, T o, boolean isDesc) {
		for (int i = 0; i < list.size(); i++) {
			if (isDesc) {
				if (((Comparable<T>) o).compareTo(list.get(i)) == 1) {
					list.add(i, o);
					return;
				}
			}else {
				if (((Comparable<T>) o).compareTo(list.get(i)) == -1) {
					list.add(i, o);
					return;
				}
			}
		}
		list.add(o);
	}
	
	/**
	 * 返回字符串长度
	 * 
	 * @param value
	 * @return
	 */
	public static int stringLength(String value) {
		int valueLength = 0;
		String chinese = "[\u0391-\uFFE5]";
		/* 获取字段值的长度,如果含中文字符,则每个中文字符长度为2,否则为1 */
		for (int i = 0; i < value.length(); i++) {
			/* 获取一个字符 */
			String temp = value.substring(i, i + 1);
			/* 判断是否为中文字符 */
			if (temp.matches(chinese)) {
				/* 中文字符长度为2 */
				valueLength += 2;
			} else {
				/* 其他字符长度为1 */
				valueLength += 1;
			}
		}
		return valueLength;
	}
	
	/**
	 * 转换字节
	 * 
	 * @param fileS
	 * @return
	 */
	public static String formetFileSize(long fileS) {
		DecimalFormat df = new DecimalFormat("#.00");
		String fileSizeString = "";
		if (fileS < 1024) {
			fileSizeString = df.format((double) fileS) + "B";
		} else if (fileS < 1048576) {
			fileSizeString = df.format((double) fileS / 1024) + "K";
		} else if (fileS < 1073741824) {
			fileSizeString = df.format((double) fileS / 1048576) + "M";
		} else {
			fileSizeString = df.format((double) fileS / 1073741824) + "G";
		}
		return fileSizeString;
	}
	
	/**
	 * 是否包装类型
	 * 
	 * @param type1
	 * @param type2
	 * @return
	 */
	public static boolean isWrappedType(Class<?> type1, Class<?> type2) {
		if (type1 == int.class && type2 == Integer.class)
			return true;
		else if (type2 == int.class && type1 == Integer.class)
			return true;
		else if (type1 == long.class && type2 == Long.class)
			return true;
		else if (type2 == long.class && type1 == Long.class)
			return true;
		else if (type1 == short.class && type2 == Short.class)
			return true;
		else if (type2 == short.class && type1 == Short.class)
			return true;
		else if (type2 == byte.class && type1 == Byte.class)
			return true;
		else if (type1 == byte.class && type2 == Byte.class)
			return true;
		else if (type2 == float.class && type1 == Float.class)
			return true;
		else if (type1 == float.class && type2 == Float.class)
			return true;
		else if (type2 == double.class && type1 == Double.class)
			return true;
		else if (type1 == double.class && type2 == Double.class)
			return true;
		else if (type2 == boolean.class && type1 == Boolean.class)
			return true;
		else if (type1 == boolean.class && type2 == Boolean.class)
			return true;

		return false;
	}
	

	
}



扩展-字符串工具类(字符串与数组、list、map、set想换转化) :http://blog.csdn.net/xiu2016/article/details/76473121



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值