`
ikeycn
  • 浏览: 144188 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
文章分类
社区版块
存档分类
最新评论

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

J# 
阅读更多
旧代码,先贴上来,待整理
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));
			}
			
		}
	}

}
分享到:
评论

相关推荐

    从源代码中抽取中文字符串的工具的源代码

    从源代码中抽取中文字符串的工具的源代码,从源代码中抽取中文字符串,修改为其他语言后替换原文件,以生成不同语言版本的软件

    java从字符串中获取数字的两种方法

    java从一个包含中文字的字符串中抽取数字部分的两种方法,自己使用后,感觉挺符合需求了,分享出来。有需要的可以看看,看是否符合需求。

    RTF文件内容的字符串读取

    从实战项目中抽取的一段代码,用于RTF文件内容的字符串读取.

    PHP字符串中抽取子串操作实例分析

    本文实例讲述了PHP字符串中抽取子串操作。分享给大家供大家参考,具体如下: 问题 希望从字符串的某个特定位置开始抽取这个字符串的一部分。例如,对于输入到一个表单的用户名,想要得到这个用户名的前8个字符。 ...

    JavaScript substr() 字符串截取函数使用详解

    substr 定义和用法 substr() 方法可在字符串中抽取从 start 下标开始的指定数目的字符。 语法 stringObject.substr(start,length) 参数 描述 start 必需。要抽取的子串的起始下标。必须是数值。如果是负数,那么...

    继承和派生向量类模板和字符串类

    给定一个头文件Vec.h,其中有抽象类模板VECROR设计。还有插入运算符重载、抽取运算符重载的普通c++函数...1、将类模板VECTOR作为基类,通过公共继承派生一个新的类模板Vector(向量类)和一个新的自定义字符串了String。

    js获取字符串最后一位方法汇总

    substr() 方法可在字符串中抽取从 start 下标开始的指定数目的字符。 代码如下: str.substr(str.length-1,1) 重要事项:ECMAscript 没有对该方法进行标准化,因此反对使用它。 重要事项:在 IE 4 中,参数 start ...

    全能字符串替换机7.0无限制版

    全能字符串替换机用于对文件进行替换、查找、抽取、改名、内码转换等操作。替换功能支持批量文件、批量串的处理,支持超长多行的查找串和替换串,支持动态的替换串;查找功能支持反显查找结果、抽取特定的查找结果;...

    提取字符串中的符号

    提取字符串中的各种符号,比如,;?等,并且还有相应的系统提示

    JavaScript 正则表达式与字符串查找方法

    如何取得一个给定的字符串substr在另一个字符串str中出现的次数? 字符串匹配,第一想到的就是正则表达式,但我们最常使用的字面量来创建的正则表达式方式却无法传入变量, 这时应该使用另一种创建正则表达式的方式...

    Java正则表达式提取字符的方法实例

    正好遇到一个需求需要将字符串中特定的字符全部提取出来,这个如果是按常规的字符串处理的话非常的繁琐。于是想到用正则表达式来完成。项目需求是这样的:需要提取车牌号中最后一个数字,比如说:苏A7865提取5,苏A...

    [Labview]将范围 0—10 的 5 个随机数转换为一个字符串显示在前面板上,要求保留 2 位小数,每个数之间用逗号分隔。

    练习题

    文本字符串批量替换/改名/查找/抽取工具亲测无毒无限制版

    在网上找了一个小时,各种下载各种试,什么文本替换专家工具挨个试了一遍,破解版也不行,不是报错就是没有批量替换大量内容的功能,注册版不知道啥情况,这个软件比较强悍还不是破解版的也不限制功能,跪谢作者,我...

    JavaScript常用截取字符串的三种方式用法区别实例解析

    stringObject.substring(start,stop) 用于提取字符串中介于两个指定下标之间的字符。...stringObject.substr(start,length)可在字符串中抽取从start下标开始的指定数目的字符 start 必需。要抽取的子串的

    hanlp分词解析字符串.zip

    HanLP分词解析字符串 在自然语言处理领域,分词是基础且重要的一步。分词的准确度直接影响到后续的文本分析、信息抽取等任务的效果。而HanLP作为一款优秀的中文自然语言处理工具,其分词功能得到了广泛的应用和认可...

    全能字符串批量替换机7.0.rar

    全能字符串替换机用于对文件进行替换、查找、抽取、改名、内码转换等操作。 替换功能支持批量文件、批量串的处理,支持超长多行的查找串和替换串,支持动态的替换串; 改名支持批量改名、按指定顺序对文件进行编号;...

    JS去掉字符串末尾的标点符号及删除最后一个字符的方法

    需求:去掉js字符串末尾的标点符号 原字符串: Hello World! 目标字符串: Hello World 方式一: stringObject.slice(start,end) start : 要抽取的片断的起始下标。如果是负数,则该参数规定的是从字符串的尾部开始...

    Linux中Shell截取某行某列信息

    背景:执行shell命令,抓取shell返回信息中的所需信息,已udhcpc为例获取DNS的地址。 暂不作处理,执行udhcpc。 使用grep过滤掉无用信息,执行 udhcpc | grep “DNS...最后使用tr去除字符串最后的\n,默认会包含。执行

Global site tag (gtag.js) - Google Analytics