/tools/src/com/cyh/StringUtil.java
Java | 540 lines | 341 code | 33 blank | 166 comment | 107 complexity | 962018e0a5e3d28fa00985ef9a401202 MD5 | raw file
- package com.cyh;
-
- import java.util.ArrayList;
- import java.util.LinkedHashSet;
- import java.util.Set;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
-
- /**
- * ?????????????????
- *
- * @author Administrator
- * @Date Jul 18, 2008
- * @Time 2:19:47 PM
- * @version 1.0
- */
- public class StringUtil {
-
- /**
- * ??????????
- *
- * @param str
- * String ?????
- * @param splitsign
- * String ???
- * @return String[] ?????????
- */
- @SuppressWarnings("unchecked")
- public static String[] split(String str, String splitsign) {
- int index;
- if (str == null || splitsign == null) {
- return null;
- }
- ArrayList al = new ArrayList();
- while ((index = str.indexOf(splitsign)) != -1) {
- al.add(str.substring(0, index));
- str = str.substring(index + splitsign.length());
- }
- al.add(str);
- return (String[]) al.toArray(new String[0]);
- }
-
- /**
- * ??????????
- *
- * @param from
- * String ?????
- * @param to
- * String ?????
- * @param source
- * String ????
- * @return String ???????
- */
- public static String replace(String from, String to, String source) {
- if (source == null || from == null || to == null)
- return null;
- StringBuffer str = new StringBuffer("");
- int index = -1;
- while ((index = source.indexOf(from)) != -1) {
- str.append(source.substring(0, index) + to);
- source = source.substring(index + from.length());
- index = source.indexOf(from);
- }
- str.append(source);
- return str.toString();
- }
-
- /**
- * ??????????HTML???????(?????????)
- *
- * @param str
- * String ?????
- * @return String ???????
- */
- public static String htmlencode(String str) {
- if (str == null) {
- return null;
- }
- return replace("\"", """, replace("<", "<", str));
- }
-
- /**
- * ?????????????????????????????
- *
- * @param str
- * String
- * @return String
- */
- public static String htmldecode(String str) {
- if (str == null) {
- return null;
- }
-
- return replace(""", "\"", replace("<", "<", str));
- }
-
- private static final String _BR = "<br/>";
-
- /**
- * ??????????????????????????????TAB
- *
- * @param str
- * String ?????
- * @return String ???????
- */
- public static String htmlshow(String str) {
- if (str == null) {
- return null;
- }
-
- str = replace("<", "<", str);
- str = replace(" ", " ", str);
- str = replace("\r\n", _BR, str);
- str = replace("\n", _BR, str);
- str = replace("\t", " ", str);
- return str;
- }
-
- /**
- * ?????????????????
- *
- * @param str
- * String ???
- * @param length
- * int ????
- * @return String ??????
- */
- public static String toLength(String str, int length) {
- if (str == null) {
- return null;
- }
- if (length <= 0) {
- return "";
- }
- try {
- if (str.getBytes("GBK").length <= length) {
- return str;
- }
- } catch (Exception e) {
- }
- StringBuffer buff = new StringBuffer();
-
- int index = 0;
- char c;
- length -= 3;
- while (length > 0) {
- c = str.charAt(index);
- if (c < 128) {
- length--;
- } else {
- length--;
- length--;
- }
- buff.append(c);
- index++;
- }
- buff.append("...");
- return buff.toString();
- }
-
- /**
- * ????????????
- *
- * @param str
- * ??????
- * @return ?????true,????false
- */
- public static boolean isInteger(String str) {
- Pattern pattern = Pattern.compile("^[-\\+]?[\\d]+$");
- return pattern.matcher(str).matches();
- }
-
- /**
- * ???????????double?float
- *
- * @param str
- * ??????
- * @return ??????true,????false
- */
- public static boolean isDouble(String str) {
- Pattern pattern = Pattern.compile("^[-\\+]?\\d+\\.\\d+$");
- return pattern.matcher(str).matches();
- }
-
- /**
- * ????????? c ??????
- */
- public static boolean isLetter(String str) {
- if (str == null || str.length() < 0) {
- return false;
- }
- Pattern pattern = Pattern.compile("[\\w\\.-_]*");
- return pattern.matcher(str).matches();
- }
-
- /**
- * ??????????Email content ??????
- *
- * @param content
- * @return
- */
- public static String parse(String content) {
- String email = null;
- if (content == null || content.length() < 1) {
- return email;
- }
- // ????@
- int beginPos;
- int i;
- String token = "@";
- String preHalf = "";
- String sufHalf = "";
-
- beginPos = content.indexOf(token);
- if (beginPos > -1) {
- // ????
- String s = null;
- i = beginPos;
- while (i > 0) {
- s = content.substring(i - 1, i);
- if (isLetter(s))
- preHalf = s + preHalf;
- else
- break;
- i--;
- }
- // ????
- i = beginPos + 1;
- while (i < content.length()) {
- s = content.substring(i, i + 1);
- if (isLetter(s))
- sufHalf = sufHalf + s;
- else
- break;
- i++;
- }
- // ?????
- email = preHalf + "@" + sufHalf;
- if (isEmail(email)) {
- return email;
- }
- }
- return null;
- }
-
- /**
- * ?????????????????Email??.
- *
- * @param str
- * ??????
- * @return ?Email????true,????false
- */
- public static boolean isEmail(String email) {
- if (email == null || email.length() < 1 || email.length() > 256) {
- return false;
- }
- Pattern pattern = Pattern
- .compile("^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$");
- return pattern.matcher(email).matches();
- }
-
- /**
- * ???????????????????
- *
- * @param str
- * ??????
- * @return ????????true,????false
- */
- public static boolean isChinese(String str) {
- Pattern pattern = Pattern.compile("[\u0391-\uFFE5]+$");
- return pattern.matcher(str).matches();
- }
-
- /**
- * ??????????,??null?""
- *
- * @param str
- * @return
- */
- public static boolean isBlank(String str) {
- return str == null || str.trim().length() == 0;
- }
-
- /**
- * ????????????
- *
- * @param x
- * @return
- */
- public static boolean isPrime(int x) {
- if (x <= 7) {
- if (x == 2 || x == 3 || x == 5 || x == 7)
- return true;
- }
- int c = 7;
- if (x % 2 == 0)
- return false;
- if (x % 3 == 0)
- return false;
- if (x % 5 == 0)
- return false;
- int end = (int) Math.sqrt(x);
- while (c <= end) {
- if (x % c == 0) {
- return false;
- }
- c += 4;
- if (x % c == 0) {
- return false;
- }
- c += 2;
- if (x % c == 0) {
- return false;
- }
- c += 4;
- if (x % c == 0) {
- return false;
- }
- c += 2;
- if (x % c == 0) {
- return false;
- }
- c += 4;
- if (x % c == 0) {
- return false;
- }
- c += 6;
- if (x % c == 0) {
- return false;
- }
- c += 2;
- if (x % c == 0) {
- return false;
- }
- c += 6;
- }
- return true;
- }
-
- /**
- * ????????????
- *
- * @param str
- * ?????
- * @return String ?????????????
- */
- public static String hangeToBig(String str) {
- double value;
- try {
- value = Double.parseDouble(str.trim());
- } catch (Exception e) {
- return null;
- }
- char[] hunit = { '?', '?', '?' }; // ??????
- char[] vunit = { '?', '?' }; // ????
- char[] digit = { '?', '?', '?', '?', '?', '?', '?', '?', '?', '?' }; // ????
- long midVal = (long) (value * 100); // ?????
- String valStr = String.valueOf(midVal); // ??????
-
- String head = valStr.substring(0, valStr.length() - 2); // ?????
- String rail = valStr.substring(valStr.length() - 2); // ?????
-
- String prefix = ""; // ?????????
- String suffix = ""; // ?????????
- // ?????????
- if (rail.equals("00")) { // ???????0
- suffix = "?";
- } else {
- suffix = digit[rail.charAt(0) - '0'] + "?"
- + digit[rail.charAt(1) - '0'] + "?"; // ?????????
- }
- // ?????????
- char[] chDig = head.toCharArray(); // ????????????
- char zero = '0'; // ??'0'?????0
- byte zeroSerNum = 0; // ????0???
- for (int i = 0; i < chDig.length; i++) { // ????????
- int idx = (chDig.length - i - 1) % 4; // ?????
- int vidx = (chDig.length - i - 1) / 4; // ????
- if (chDig[i] == '0') { // ???????0
- zeroSerNum++; // ??0????
- if (zero == '0') { // ??
- zero = digit[0];
- } else if (idx == 0 && vidx > 0 && zeroSerNum < 4) {
- prefix += vunit[vidx - 1];
- zero = '0';
- }
- continue;
- }
- zeroSerNum = 0; // ??0????
- if (zero != '0') { // ??????0,???,???,????
- prefix += zero;
- zero = '0';
- }
- prefix += digit[chDig[i] - '0']; // ???????
- if (idx > 0)
- prefix += hunit[idx - 1];
- if (idx == 0 && vidx > 0) {
- prefix += vunit[vidx - 1]; // ?????????????,?
- }
- }
-
- if (prefix.length() > 0)
- prefix += '?'; // ????????,??????
- return prefix + suffix; // ??????
- }
-
- /**
- * ??????????????????
- *
- * @param str
- * ?????????????????????????
- * @return String ???????????????
- */
- private static String removeSameString(String str) {
- Set<String> mLinkedSet = new LinkedHashSet<String>();// set??????????????
- String[] strArray = str.split(" ");// ????(?????)?????
- StringBuffer sb = new StringBuffer();
-
- for (int i = 0; i < strArray.length; i++) {
- if (!mLinkedSet.contains(strArray[i])) {
- mLinkedSet.add(strArray[i]);
- sb.append(strArray[i] + " ");
- }
- }
- System.out.println(mLinkedSet);
- return sb.toString();
- }
-
- /**
- * ???????????
- *
- * @param src
- * @return
- */
- public static String encoding(String src) {
- if (src == null)
- return "";
- StringBuilder result = new StringBuilder();
- if (src != null) {
- src = src.trim();
- for (int pos = 0; pos < src.length(); pos++) {
- switch (src.charAt(pos)) {
- case '\"':
- result.append(""");
- break;
- case '<':
- result.append("<");
- break;
- case '>':
- result.append(">");
- break;
- case '\'':
- result.append("'");
- break;
- case '&':
- result.append("&");
- break;
- case '%':
- result.append("&pc;");
- break;
- case '_':
- result.append("&ul;");
- break;
- case '#':
- result.append("&shap;");
- break;
- case '?':
- result.append("&ques;");
- break;
- default:
- result.append(src.charAt(pos));
- break;
- }
- }
- }
- return result.toString();
- }
-
- /**
- * ?????????????????
- *
- * @param handset
- * @return boolean
- */
- public static boolean isHandset(String handset) {
- try {
- String regex = "^1[\\d]{10}$";
- Pattern pattern = Pattern.compile(regex);
- Matcher matcher = pattern.matcher(handset);
- return matcher.matches();
-
- } catch (RuntimeException e) {
- return false;
- }
- }
-
- /**
- * ????????????
- *
- * @param src
- * @return
- */
- public static String decoding(String src) {
- if (src == null)
- return "";
- String result = src;
- result = result.replace(""", "\"").replace("'", "\'");
- result = result.replace("<", "<").replace(">", ">");
- result = result.replace("&", "&");
- result = result.replace("&pc;", "%").replace("&ul", "_");
- result = result.replace("&shap;", "#").replace("&ques", "?");
- return result;
- }
-
- /**
- * @param args
- */
- public static void main(String[] args) {
- // String source = "abcdefgabcdefgabcdefgabcdefgabcdefgabcdefg";
- // String from = "efg";
- // String to = "???";
- // System.out.println("????source???to??from???????"
- // + replace(from, to, source));
- // System.out.println("?????????????"
- // + toLength("abcdefgabcdefgabcdefgabcdefgabcdefgabcdefg", 9));
- // System.out.println("????????" + isInteger("+0"));
- // System.out.println("???????????double?float?" + isDouble("+0.36"));
- // System.out.println("????????????Email???" +
- // isEmail("fhwbj@163.com"));
- // System.out.println("???????????????" + isChinese("???"));
- // System.out.println("?????????????" + isPrime(12));
- // System.out.println("?????????" + hangeToBig("10019658"));
- System.out.println("??????????????" + removeSameString("100 100 9658"));
- // System.out.println("???????" + encoding("100\"s<>fdsd100 9658"));
- // System.out.println("?????????????" + isHandset("15981807340"));
-
- // System.out.println("???????Email?" + parse("159818 fwhbj@163.com
- // 07340"));
- }
- }