---
title: "字符串中抽取某类型的字符串"
author: "ikeycn"
date: "2010-10-24T00:05:29Z"
tags: ["J#"]
categories: ["编程语言"]
slug: "untitled"
draft: false
lastmod: "2010-10-24T00:05:29Z"
id: 792378
url: "https://ikeycn.iteye.com/blog/792378"
---

旧代码，先贴上来，待整理 \[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 &lt; bytes.length;) { if (bytes\[i] &lt; 0) { bc\[j++] = bytes\[i++]; bc\[j++] = bytes\[i]; } i++; } byte\[] br = new byte\[j]; for (int i = 0; i &lt; 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 &lt; bytes.length;) { if ((bytes\[i] &gt;= 0x41 && bytes\[i] &lt;= 0x5a) || (bytes\[i] &gt;= 0x61 && bytes\[i] &lt;= 0x7a)) { bc\[j++] = bytes\[i]; } i++; } byte\[] br = new byte\[j]; for (int i = 0; i &lt; 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 &lt; bytes.length;) { if (bytes\[i] &gt;= 0x30 && bytes\[i] &lt;= 0x39) { bc\[j++] = bytes\[i]; } i++; } byte\[] br = new byte\[j]; for (int i = 0; i &lt; 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 &gt; 3) { throw new ArrayIndexOutOfBoundsException(); } else { for (int i = 0; i &lt; 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]