JAVA工具類(二)

/*
 * Copyright (c) 2005 Your Corporation. All Rights Reserved.
 */
package com.chis;

/**
 * Created by IntelliJ IDEA. User: Shi Tiejun Date: 2005-12-1 Time: 10:35:46 To
 * change this template use File | Settings | File Templates.
 */

public class StringFormatUtil {
	/**
	 * 產生上級區劃國標
	 * 
	 * @param zoneCode
	 * @return 上級 區劃國標
	 */
	public static String getParentZoneCode(String zoneCode) {
		String s = StringFormatUtil.dealZero(zoneCode);
		s = s.substring(0, s.length() - 2);
		s = s + "00000000";
		s = s.substring(0, 8);
		return s;
	}

	/**
	 * 產生下級區劃國標查詢串
	 * 
	 * @param zoneCode
	 * @return 產生下級區劃國標查詢串 oracle中單字匹配符號_
	 */
	public static String getChildZoneCode(String zoneCode) {
		String s = StringFormatUtil.dealZero(zoneCode);
		s = s + "__00000000";
		s = s.substring(0, 8);
		return s;
	}

	/**
	 * 根據傳入的url獲取頁面文件
	 * 
	 * @param urlStr
	 * @return pagename
	 */
	public static String getUrlPageName(String urlStr) {
		String pageName = "";
		String[] tempUrl = urlStr.split("\\?");
		if (tempUrl.length > 0) {
			pageName = tempUrl[0];
			while (pageName.indexOf("/", 0) > -1) {
				pageName = pageName.substring(pageName.indexOf("/", 0) + 1);
			}
			String[] tempParameter = pageName.split("\\.");
			if (tempParameter.length > 1) {
				if (!tempParameter[1].equalsIgnoreCase("jsp")) {
					pageName = "";
				}
			}
		}

		return pageName;

	}

	/**
	 * 獲取DisplayTag分頁的參數名稱和頁數
	 * 
	 * @param urlStr
	 * @return String[] 參數名稱 參數值
	 */
	public static String[] getDisplayDP(String urlStr) {
		String parameter, parameterName, parameterValue;
		parameter = "";
		parameterName = "";
		parameterValue = "";
		String[] tempUrl = urlStr.split("\\?");
		if (tempUrl.length > 1) {
			parameter = tempUrl[1];
			String[] tempParameter = parameter.split("&");
			for (int i = 0; i < tempParameter.length; i++) {
				String[] temp = tempParameter[i].split("=");
				if (temp[0].matches("d-[0-9]+-p")) {
					parameterName = temp[0];
					parameterValue = temp[1];
					break;
				}
			}
		}
		String[] returnvalue = { parameterName, parameterValue };
		return returnvalue;
	}

	/**
	 * 根據傳入的行政區劃國標編碼取得地區級別
	 * 
	 * @param zoneCode
	 * @return String 0處理錯誤 1國家,2省市,3地市,4縣區、5鄉鎮
	 */
	public static String getZoneLevel(String zoneCode) {
		String returnValue = "";
		if (zoneCode.length() != 8) {
			returnValue = "0";
		} else {
			returnValue = "5";
			if (zoneCode.substring(zoneCode.length() - 2).equals("00")) {
				zoneCode = zoneCode.substring(0, 6);
				returnValue = "4";
			}
			if (zoneCode.substring(zoneCode.length() - 2).equals("00")) {
				zoneCode = zoneCode.substring(0, 4);
				returnValue = "3";
			}
			if (zoneCode.substring(zoneCode.length() - 2).equals("00")) {
				zoneCode = zoneCode.substring(0, 2);
				returnValue = "2";
			}
		}
		return returnValue;
	}

	/**
	 * 去除傳入的行政區劃國標編碼兩位零
	 * 
	 * @param zoneCode
	 * @return string
	 */
	public static String dealZero(String zoneCode) {
		if (zoneCode.length() != 8) {
			return zoneCode;
		} else {
			if (zoneCode.substring(zoneCode.length() - 2).equals("00")) {
				zoneCode = zoneCode.substring(0, 6);
			}
			if (zoneCode.substring(zoneCode.length() - 2).equals("00")) {
				zoneCode = zoneCode.substring(0, 4);
			}
			if (zoneCode.substring(zoneCode.length() - 2).equals("00")) {
				zoneCode = zoneCode.substring(0, 2);
			}
			return zoneCode;
		}
	}
	/**
	 * 去除傳入的行政區劃國標編碼兩位零
	 * 
	 * @param zoneCode
	 * @return string
	 */
	public static String dealLongZero(String zoneCode) {
		if (zoneCode.length() != 10) {
			return zoneCode;
		} else {
			if (zoneCode.substring(zoneCode.length() - 2).equals("00")) {
				zoneCode = zoneCode.substring(0, 8);
			}
			if (zoneCode.substring(zoneCode.length() - 2).equals("00")) {
				zoneCode = zoneCode.substring(0, 6);
			}
			if (zoneCode.substring(zoneCode.length() - 2).equals("00")) {
				zoneCode = zoneCode.substring(0, 4);
			}
			if (zoneCode.substring(zoneCode.length() - 2).equals("00")) {
				zoneCode = zoneCode.substring(0, 2);
			}
			return zoneCode;
		}
	}

	/**
	 * 去除多餘的0
	 * 
	 * @param str
	 * @return String
	 */
	public static String trimZero(String str) {
		String dealZero = "";
		String tag = "no";
		int size = 0;
		for (int i = 0; i < str.length(); i++) {
			if (str.charAt(i) == '0') {
				for (int j = i; j < str.length(); j++) {
					if (str.charAt(j) == '0') {
						size = i;
						if (j == (str.length() - 1)) {
							tag = "yes";
						}
					} else {
						size = 0;
						break;
					}
				}
			}
			if (tag.equals("yes")) {
				break;
			}
		}
		if (size != 0) {
			dealZero = str.substring(0, size);
		} else {
			dealZero = str;
		}
		return dealZero;
	}

	/**
	 * add by zhouzw 根據傳入的參數獲取子標題的名稱 用於醫療資源
	 */
	public static String getSubTitle(String titleid) {
		String myTitle = "";
		if ("1".equals(titleid)) {
			myTitle = "出版論著";
		} else if ("2".equals(titleid)) {
			myTitle = "發表論文";
		} else if ("3".equals(titleid)) {
			myTitle = "獲獎科研項目";
		} else if ("4".equals(titleid)) {
			myTitle = "進修實習人數";
		}

		return myTitle;
	}

	/**
	 * 16進制顯示數據
	 * 
	 * @param value
	 *            字節數組
	 * @return
	 */
	public static String toHexString(byte[] value) {
		String newString = "";
		for (int i = 0; i < value.length; i++) {
			byte b = value[i];
			String str = Integer.toHexString(b);
			if (str.length() > 2) {
				str = str.substring(str.length() - 2);
			}
			if (str.length() < 2) {
				str = "0" + str;
			}
			newString += str;
		}
		return newString.toUpperCase();
	}

	/**
	 * 把16進制字符串轉換成字節數組
	 * 
	 * @param hex
	 * @return
	 */
	public static byte[] hexStringToByte(String hex) {
		int len = (hex.length() / 2);
		byte[] result = new byte[len];
		char[] achar = hex.toCharArray();
		for (int i = 0; i < len; i++) {
			int pos = i * 2;
			result[i] = (byte) (toByte(achar[pos]) << 4 | toByte(achar[pos + 1]));
		}
		return result;
	}

	private static byte toByte(char c) {
		byte b = (byte) "0123456789ABCDEF".indexOf(c);
		return b;
	}

	/**
	 * 轉化十六進制編碼爲字符串
	 * 
	 * @param s
	 * @return
	 */
	public static String toStringHex(String s) {
		byte[] baKeyword = new byte[s.length() / 2];
		for (int i = 0; i < baKeyword.length; i++) {
			try {
				baKeyword[i] = (byte) (0xff & Integer.parseInt(s.substring(i * 2, i * 2 + 2), 16));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		try {
			s = new String(baKeyword, "utf-8 ");// UTF-16le:Not
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		return s;
	}

}

發佈了28 篇原創文章 · 獲贊 2 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章