检查中文字符小程序

写各种java或jsp的时候,一不小心就会多写了一些中文的标点字符,特别是jsp中的javascript,里面出了错都没有提示,郁闷的要死。

 

心想要是有这么一个检查中文字符小程序就好了。网上找了很久,都没找到自己想要的。于是自己写:

 

注:主要是检查',' ,'。' ,'、',';','’','‘','”','“',':' 这些字符,不让它秘密的隐藏在jsp中

 

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;


public class UnsupportedCharCheck{

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		try {
			test("D:/helloworld/index.jsp");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void test(String articleLoction) throws IOException {
		File article = new File(articleLoction);
		char[] unsupportedChars = { ',' ,'。' ,'、',';','’','‘','”','“',':'};//' '不能表示中文空格?那..
		Map unsupportedCharsAndLineNo = new HashMap();
		Map unsupportedCharsFound = new HashMap();
		int lineNum = 0;
		int commentedNum = 0;
		int multiCommentBegin = -1;//标注多行注释<!--的起始位置
		int multiCommentEnd = -1;//标注多行注释<!--的起始位置
		boolean mc = false;
		boolean problem_flag = false;
		BufferedReader bf = new BufferedReader(new FileReader(article));
		
		String lineStr = "";
		while((lineStr=bf.readLine())!=null){
			lineNum++;
			if(lineStr.startsWith("//"))continue;

			if(mc)
				if(((multiCommentEnd=lineStr.indexOf("-->")))==-1){
					continue;
				}else {
					lineStr = lineStr.substring(multiCommentEnd+3);
					mc = false;
				}
			
			while((multiCommentBegin=lineStr.indexOf("<!--"))!=-1){
				if(((multiCommentEnd=lineStr.indexOf("-->")))!=-1){					
					lineStr = lineStr.substring(0, multiCommentBegin)+lineStr.substring(multiCommentEnd+3);
					mc = false;
				}else {
					lineStr = lineStr.substring(0, multiCommentBegin);
					mc = true;
				}
			}
			
			if(mc)
				if(((multiCommentEnd=lineStr.indexOf("-->")))==-1){
					continue;
				}else {
					lineStr = lineStr.substring(multiCommentEnd+3);
					mc = false;
				}
			//System.out.println(lineStr);
			
			
			int commentBegin = 0; 
			String errStr = "";
			for(int i=0;i<lineStr.length();i++) {
				if(commentBegin==2)break;
				if(multiCommentBegin!=-1);
				char c = lineStr.charAt(i);
				if(c=='/'){
					commentBegin += 1;
					if(commentBegin == 2) {
						 commentedNum ++;
						 commentBegin = 0;
					}
					continue;
				}else if( commentBegin == 1){
					commentBegin = 0;
				}
				
				for(int j=0;j<unsupportedChars.length;j++) {
					if(c==unsupportedChars[j]) {
						if(errStr.length()==0) {
							errStr += c;
						}else {
							errStr += "|" + c;
						}
						
						if(!unsupportedCharsFound.containsKey(c)) {
							unsupportedCharsFound.put(c,lineNum);
							//System.out.println("FOUND UNSUPPORTED CHAR: '" +  c  +"' AT LINE " +lineNum);
						}else{
							//System.out.println("FOUND UNSUPPORTED CHAR: '" +  c  +"' AT LINE " +lineNum);
						}
						unsupportedCharsAndLineNo.put(lineNum, c);
						break;
					}
				}
			}
			if(errStr.length()!=0) {
				problem_flag = true;
				System.out.println("FOUND UNSUPPORTED CHARS: " +  errStr  +" AT LINE " +lineNum);
			}
		}
		if(!problem_flag)System.out.println("CONGRATULATIONS!  NO UNSUPPORTED CHARS FOUND~~");
		
		
	}
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值