java 特殊符号过滤_过滤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

分享到:

18e900b8666ce6f233d25ec02f95ee59.png

72dd548719f0ace4d5f9bca64e1d7715.png

2014-02-28 23:11

浏览 63

评论

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值