分别统计中英文字符
package lianxi; import java.util.HashMap;
import java.util.*;
import java.util.Map; /**
?*
?* 如果一串字符串如"aaaabbbc中個512",分別統計英文字符,
?* 中文字符,和數字的字符的數量*
?*
?* 也可以自己進行一些排序,通過hashSet來實現和complaretor接口可以實現排序
?*
?* */
public class SubStrCount {
?
?
? private int digCount=0;
? private int englishCount=0;
? private int chinseCount=0;
???
?? public Map digMap;
?? public Map chinseMap;
?? public Map englishMap;
??
?? public static void main(String[]args){
???
??? String tempstr="aaaabbbc中個512";
???
??? SubStrCount test= new SubStrCount();
???
??? test.getCount(tempstr);
???
??? if(test.digMap!=null){
?????? System.out.println("數字的個數:"+(test.digMap.size()));
?????? System.out.println("---------------------------------");???
?????? test.println(test.digMap);
??????
??? }
??? if(test.chinseMap!=null){
??????? System.out.println("中文的個數:"+(test.chinseMap.size()));
??????? System.out.println("---------------------------------");
??????? test.println(test.chinseMap);
??? }
??? if(test.englishMap!=null){
??????? System.out.println("字母的個數:"+(test.englishMap.size()));
??????? System.out.println("---------------------------------");
??????? test.println(test.englishMap);
??? }
???
?? }
??
? public void println(Map tempMap){
??
?? if(tempMap!=null){
???
??? Iterator tempiterator= tempMap.keySet().iterator();
???
??? while(tempiterator.hasNext()){
????
???? String tempchar=(String)tempiterator.next();
????
???? System.out.println(tempchar+" :"+tempMap.get(tempchar));
????
??? }
???
?? }??
? }
?
?
?? public void getCount(String str){??
??? digMap=new HashMap();
??? chinseMap=new HashMap();
??? englishMap=new HashMap();???
???
??? char []array=str.toCharArray();
???
??? for(int i=0;i<array.length;i++){
????
???? if((array[i]>='a' && array[i]<='z')||(array[i]>='A' && array[i]<='Z')){????
????? addMap(englishMap,String.valueOf(array[i]));
????? englishCount++;?????
???? }
???? else if(array[i]>='0' && array[i]<='9'){
????? addMap(digMap,String.valueOf(array[i]));
????? digCount++;
???? }
???? else{
????? addMap(chinseMap,String.valueOf(array[i]));
????? chinseCount++;?????
???? }
??? }???
?? }
??
?? public void addMap(Map tempMap,String tempChar){//根據不同的數據來存儲
??? if(tempMap!=null){
????
???? Integer integer=(Integer)tempMap.get(tempChar);
????
???? if(integer==null){??
?????
????? tempMap.put(tempChar,1);?????
???? }
???? else{
?????
????? tempMap.put(tempChar,integer+1);
???? }
??? }
???
?? }
??
}
import java.util.*;
import java.util.Map; /**
?*
?* 如果一串字符串如"aaaabbbc中個512",分別統計英文字符,
?* 中文字符,和數字的字符的數量*
?*
?* 也可以自己進行一些排序,通過hashSet來實現和complaretor接口可以實現排序
?*
?* */
public class SubStrCount {
?
?
? private int digCount=0;
? private int englishCount=0;
? private int chinseCount=0;
???
?? public Map digMap;
?? public Map chinseMap;
?? public Map englishMap;
??
?? public static void main(String[]args){
???
??? String tempstr="aaaabbbc中個512";
???
??? SubStrCount test= new SubStrCount();
???
??? test.getCount(tempstr);
???
??? if(test.digMap!=null){
?????? System.out.println("數字的個數:"+(test.digMap.size()));
?????? System.out.println("---------------------------------");???
?????? test.println(test.digMap);
??????
??? }
??? if(test.chinseMap!=null){
??????? System.out.println("中文的個數:"+(test.chinseMap.size()));
??????? System.out.println("---------------------------------");
??????? test.println(test.chinseMap);
??? }
??? if(test.englishMap!=null){
??????? System.out.println("字母的個數:"+(test.englishMap.size()));
??????? System.out.println("---------------------------------");
??????? test.println(test.englishMap);
??? }
???
?? }
??
? public void println(Map tempMap){
??
?? if(tempMap!=null){
???
??? Iterator tempiterator= tempMap.keySet().iterator();
???
??? while(tempiterator.hasNext()){
????
???? String tempchar=(String)tempiterator.next();
????
???? System.out.println(tempchar+" :"+tempMap.get(tempchar));
????
??? }
???
?? }??
? }
?
?
?? public void getCount(String str){??
??? digMap=new HashMap();
??? chinseMap=new HashMap();
??? englishMap=new HashMap();???
???
??? char []array=str.toCharArray();
???
??? for(int i=0;i<array.length;i++){
????
???? if((array[i]>='a' && array[i]<='z')||(array[i]>='A' && array[i]<='Z')){????
????? addMap(englishMap,String.valueOf(array[i]));
????? englishCount++;?????
???? }
???? else if(array[i]>='0' && array[i]<='9'){
????? addMap(digMap,String.valueOf(array[i]));
????? digCount++;
???? }
???? else{
????? addMap(chinseMap,String.valueOf(array[i]));
????? chinseCount++;?????
???? }
??? }???
?? }
??
?? public void addMap(Map tempMap,String tempChar){//根據不同的數據來存儲
??? if(tempMap!=null){
????
???? Integer integer=(Integer)tempMap.get(tempChar);
????
???? if(integer==null){??
?????
????? tempMap.put(tempChar,1);?????
???? }
???? else{
?????
????? tempMap.put(tempChar,integer+1);
???? }
??? }
???
?? }
??
}
轉載于:https://blog.51cto.com/zhaohaiyang/514327
總結
- 上一篇: Boke宝贝 - 免费提供图片存储空间、
- 下一篇: Js+Css打造的红色经典伸缩菜单代码