字符串中抽取某类型的字符串
旧代码,先贴上来,待整理 [code="java"] import java.io.UnsupportedEncodingException; import java.util.HashMap; import java.util.Map; import java.util.Set; public class StringHandler { public static final int CH_STR = 1; public static final int ALPHA_STR = 2; public static final int NUM_STR = 3; /** * 1.抽取中文字符串 * * @param str * @return * @throws UnsupportedEncodingException */ public String extraChinese(String str) throws UnsupportedEncodingException { String result; byte[] bytes = str.getBytes("gbk"); byte[] bc = new byte[bytes.length]; int j = 0; for (int i = 0; i < bytes.length;) { if (bytes[i] < 0) { bc[j++] = bytes[i++]; bc[j++] = bytes[i]; } i++; } byte[] br = new byte[j]; for (int i = 0; i < j; i++) { br[i] = bc[i]; } result = new String(br, "gbk"); return result; } /** * 抽取字母 * @param str * @return * @throws UnsupportedEncodingException */ public String extraAlpha(String str) throws UnsupportedEncodingException { String result; byte[] bytes = str.getBytes("gbk"); byte[] bc = new byte[bytes.length]; int j = 0; for (int i = 0; i < bytes.length;) { if ((bytes[i] >= 0x41 && bytes[i] <= 0x5a) || (bytes[i] >= 0x61 && bytes[i] <= 0x7a)) { bc[j++] = bytes[i]; } i++; } byte[] br = new byte[j]; for (int i = 0; i < j; i++) { br[i] = bc[i]; } result = new String(br, "gbk"); return result; } /** * 抽取数字 * @param str * @return * @throws UnsupportedEncodingException */ public String extraNum(String str) throws UnsupportedEncodingException { String result; byte[] bytes = str.getBytes("gbk"); byte[] bc = new byte[bytes.length]; int j = 0; for (int i = 0; i < bytes.length;) { if (bytes[i] >= 0x30 && bytes[i] <= 0x39) { bc[j++] = bytes[i]; } i++; } byte[] br = new byte[j]; for (int i = 0; i < j; i++) { br[i] = bc[i]; } result = new String(br, "gbk"); return result; } /** * 2.根据类型抽取字符串 * @param str * @param type * @return * @throws UnsupportedEncodingException */ public String extraStr(String str, int type) throws UnsupportedEncodingException { String result; if (this.CH_STR == type) { result = extraChinese(str); } else if (this.ALPHA_STR == type) { result = this.extraAlpha(str); } else if (this.NUM_STR == type) { result = this.extraNum(str); } else { result = ""; } return result; } /** * 3 根据多个类型抽取多个字符串组 * @param str * @param types * @return * @throws UnsupportedEncodingException */ public Map extraStr(String str, int... types) throws UnsupportedEncodingException { Map map = new HashMap(); if (types.length > 3) { throw new ArrayIndexOutOfBoundsException(); } else { for (int i = 0; i < types.length; i++) { map.put(types[i], extraStr(str, types[i])); } } return map; } /** * @param args * @throws UnsupportedEncodingException */ public static void main(String[] args) throws UnsupportedEncodingException { // TODO Auto-generated method stub StringHandler sh = new StringHandler(); String test = new String("hello你world好2a!"); //test1 System.out.println(sh.extraChinese(test)); //test2 System.out.println(sh.extraStr(test, ALPHA_STR)); //test3 Map result = sh.extraStr(test, ALPHA_STR, CH_STR, NUM_STR); Set types = result.keySet(); for(int type : types){ if(type == StringHandler.ALPHA_STR){ System.out.println("字母:" + result.get(type)); }else if (type == StringHandler.CH_STR){ System.out.println("中文:" + result.get(type)) ; }else{ System.out.println("数字:" + result.get(type)); } } } }[/code]