匹配字符串中的连续多少位数字

package com.zte.xh.fund.util;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.jfinal.plugin.activerecord.Record;
import com.mysql.jdbc.StringUtils;
import com.zte.xh.fund.model.FundFinance;

/**
 * 对账的工具类
 * 
 * @author Jay_Lee
 *
 */
public class CheckoutUtil {
	/**
	 * 匹配出字符串中的6,8,10位Id
	 * 
	 * @param str
	 * @return
	 */
	public static String matchId(String str) {
		// 匹配6,8,10位的工号
		String regEx = null;
		Pattern p = null;
		Matcher m = null;
		String result = null;
		// 首先匹配10位的
		regEx = "[0-9]{10}";
		p = Pattern.compile(regEx);
		m = p.matcher(str);
		while (m.find()) {
			result = m.group();
		}
		// 如果10位的为空就匹配8位的
		if (StringUtils.isNullOrEmpty(result)) {
			regEx = "[0-9]{8}";
			p = Pattern.compile(regEx);
			m = p.matcher(str);
			while (m.find()) {
				result = m.group();
			}
		}
		// 如果8位为空,就匹配6位
		if (StringUtils.isNullOrEmpty(result)) {
			regEx = "[0-9]{6}";
			p = Pattern.compile(regEx);
			m = p.matcher(str);
			while (m.find()) {
				result = m.group();
			}
			// 6位的工号,在第1位后面加2个0,补齐成新工号
			if (!StringUtils.isNullOrEmpty(result)) {
				String start = result.substring(0, 1);
				String end = result.substring(1, result.length());
				result = start + "00" + end;
			}
		}
		return StringUtils.isNullOrEmpty(result) ? "" : result;
	}

	/**
	 * 发现相同的工号,便于合并
	 * 
	 * @param records
	 * @return
	 */
	public static List<List<Integer>> findRepeat(List<Record> records) {
		List<List<Integer>> result = new ArrayList<List<Integer>>();
		String demp = "";
		for (int i = 0; i < records.size(); i++) {
			String[] demps = demp.split(",");
			if (breakFor(demps, i)) {
				continue;
			}
			List<Integer> result_ = new ArrayList<Integer>();
			String startTemp = records.get(i).get("userId");
			if (startTemp == null || startTemp.length() <= 0) {
				continue;
			}
			result_.add(i);
			for (int j = i + 1; j < records.size(); j++) {
				if (breakFor(demps, j)) {
					continue;
				}
				String endTemp = records.get(j).get("userId");
				if (startTemp.equals(endTemp)) {
					result_.add(j);
					demp += (j + ",");
				}
			}
			if (result_.size() > 1) {
				result.add(result_);
			}
		}
		return result;
	}

	/**
	 * 验证数组中是否包含了某个数据
	 * 
	 * @param strs
	 * @param i
	 * @return
	 */
	public static boolean breakFor(String[] strs, int i) {
		for (String s : strs) {
			if (s.equals(String.valueOf(i))) {
				return true;
			}
		}
		return false;
	}

	/**
	 * 把list转换成string,string的形式
	 * 
	 * @param list
	 * @return
	 */
	public static String listToString(List<?> list) {
		String result = "";
		for (Object str : list) {
			result += String.valueOf(str) + ",";
		}
		result = result.substring(0, result.length() - 1);
		return result;
	}

	/**
	 * 算出map对应的value的和
	 * 
	 * @param map
	 * @return
	 */
	public static String getAccount(Map<Integer, String> map) {
		double d = 0;
		for (Entry<Integer, String> entry : map.entrySet()) {
			d += Double.valueOf(StringUtils.isNullOrEmpty(entry.getValue())?"0":entry.getValue());
		}
		return String.valueOf(d);
	}

	/**
	 * 格式化金额。把20000.00按照万为单位格式化为2
	 * 
	 * @param money
	 * @return
	 */
	public static String formatMoney(String money) {
		int i = money.indexOf(".");//i=-1表示不存在
		String result = null;
		if(i!=-1){
			result = money.substring(0, money.indexOf("."));
			if(result.equals("0")){
				result = "0";
			}else{
				result=result.substring(0, result.length() - 4);
			}
		}else{
			if(money.equals("0")){
				result = "0";
			}else{
				result = money.substring(0, money.length() - 4);
			}
			
		}
		return result;
	}
	
	/**
	 * 初始化fund,因为数据库表不能为null,在此做共有的初始化
	 */
	public static void initFund(FundFinance fFinance){
		fFinance.set("user_name", "");
		fFinance.set("cert_id", "");
		fFinance.set("tel", "");
		fFinance.set("count", 0);
		fFinance.set("k_name", "");
		fFinance.set("bank_name", "");
		fFinance.set("bank_num", "");
		// 3表示已提交登记
		fFinance.set("status", "3");
		// 下面的都不知道是什么.又不能为空
		fFinance.set("add_time", System.currentTimeMillis());
		fFinance.set("s_time", "");
		fFinance.set("shenming", "");
		fFinance.set("z_name", "");
		fFinance.set("his_shouyi", 0);
		fFinance.set("z_num", "");
		fFinance.set("images", "");
		fFinance.set("images2", "");
		fFinance.set("user_pay_time", 0);
		fFinance.set("pay_message", "");
		fFinance.set("sure_info", "");
		fFinance.set("p_beizhu", "");
		fFinance.set("cancel_time", 0);
		fFinance.set("cancel_info", "");
		fFinance.set("update_time", 0);
		fFinance.set("update_info", "");
		fFinance.set("s_time", 0);
	}
	/**
	 * 测试方法
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		/*
		 * String test = "行内汇入 001娄卫华  娄卫华 +123456+ 爱心理财"; List<Integer> testL =
		 * new ArrayList<Integer>(); testL.add(1); testL.add(2); testL.add(3);
		 * testL.add(5); Map<Integer, String> map = new HashMap<Integer,
		 * String>(); map.put(1, "30000.0"); map.put(2, "50000.0"); map.put(3,
		 * "30000.0"); System.out.println(getAccount(map));
		 * System.out.println(listToString(testL));
		 * System.out.println(matchId(test));
		 */
	/*	String s = "11111,11111,";
		s = s.substring(0,s.length()-1);
		System.out.println(s);*/
		System.out.println(formatMoney("200000"));
	}
}

猜你喜欢

转载自lijie-insist.iteye.com/blog/2233470