过滤Java中特殊字符

过滤Java中特殊字符

/**
 * @Title:FilterString.java
 * @Package:com.you.model
 * @Description:过滤Java中特殊字符
 * @Author: 游海东
 * @date: 2014年2月28日 下午10:58:47
 * @Version V1.2.3
 */
package com.you.model;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

/**
 * @类名:FilterString
 * @描述:过滤Java中特殊字符
 * @Author:Administrator
 * @date: 2014年2月28日 下午10:58:47
 */
public class FilterString 
{
	/**
	 * 判断特殊字符
	 * @Title : FilterStr
	 * @Type : FilterString
	 * @date : 2014年2月28日 下午11:01:21
	 * @Description : 判断特殊字符
	 * @param str
	 * @return
	 * @throws PatternSyntaxException
	 */
	public static String FilterStr(String str) throws PatternSyntaxException
	{
		/**
		 * 特殊字符
		 */
		String regEx="[`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~!@#¥%……&*()——+|{}【】‘;:”“’。,、?_]";
		
        /**
         * Pattern p = Pattern.compile("a*b");
         * Matcher m = p.matcher("aaaaab");
         * boolean b = m.matches();
         */
		Pattern pat = Pattern.compile(regEx);     
        Matcher mat = pat.matcher(str);
        
        /**
         * 返回替换结果
         */
        return mat.replaceAll("").trim();  
	}

	/**
	 * @Title : main
	 * @Type : FilterString
	 * @date : 2014年2月28日 下午10:58:47
	 * @Description : 过滤字符
	 * @param args
	 */
	public static void main(String[] args) 
	{
		/**
		 * 测试字符串
		 */
		String totalStr = "~`<>?^&*()you@##%$$#^%^h^&&*&*()<>?ai@#@$~~`_+|dong?><:";
		/**
		 * 打印测试字符串
		 */
		System.out.println("打印测试字符串:" + totalStr);
		
		/**
		 * 调用过滤字符串的方法
		 */
		String filterStr = FilterStr(totalStr);
		/**
		 * 打印过滤字符串
		 */
		System.out.println("打印过滤字符串:" + filterStr);
	}

}

测试结果:
打印测试字符串:~`<>?^&*()you@##%$$#^%^h^&&*&*()<>?ai@#@$~~`_+|dong?><:
打印过滤字符串:youhaidong


再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow

猜你喜欢

转载自www.cnblogs.com/odejsjhshw/p/10382825.html