字符串中抽取某类型的字符串

旧代码,先贴上来,待整理

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<Integer, String> extraStr(String str, int... types)
throws UnsupportedEncodingException {
Map<Integer, String> map = new HashMap<Integer, String>();
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<Integer, String> result = sh.extraStr(test, ALPHA_STR, CH_STR, NUM_STR);
Set<Integer> 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));
}

}
}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值