文字列からタイプを抽出する文字列
4781 ワード
古いコード、先に貼って、整理します
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));
}
}
}
}