區別輸入的字符是英文的還是中文 或者是 特殊字符

區別中文和英文字符的方法:

String str = "我愛你,xr";
	        char[] array = str.toCharArray();
	        int chineseCount = 0;
	        int englishCount = 0;
	        for (int i = 0; i < array.length; i++) {
	            if((char)(byte)array[i]!=array[i]){
	                chineseCount++;
	            }else{
	                englishCount++;
	            }
	        }

判斷是否包含特殊字符的方法:

Pattern p = Pattern.compile("[&%$#@!(),*^]"); //可在此加上其他的特殊字符
	    	Matcher m = p.matcher(str);
	    	if (m.find()) {
	    		Toast.makeText(getApplicationContext(),"you can not enter special Character", 1).show();
	    	} else {
	    		Toast.makeText(getApplicationContext(),"input ok", 1).show();
			}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章