java字符串常用操作

1.計算一個字符串中各個字符出現的次數:

public class CountCharFromStr {

        public static void main(String[] args) {

                String str = "abcedsadgsadfasdfaa";

                TreeMap<Character,Integer> map=new TreeMap<Character,Integer>();

                map=count(str);

                for(Entry<Character,Integer> entry:map.entrySet()){

                        System.err.println(entry.getKey()+"----"+entry.getValue());

                }

        }

      public static TreeMap<Character,Integer> count(String str){

            char[] arr=str.toCharArray();

            TreeMap<Character,Integer> map=new TreeMap<Character,Integer>();

            for(char ch:arr){

                    if(!map.containsKey(ch)){

                            map.put(ch,1);

                    }else{

                            int count=map.get(ch)+1;

                            map.put(ch, count);
                    }
            }
              return map;

    }

}

結果爲:a----6    b----1    c----1    d----4    e----1    f----2    g----1    s----3

2.截取字符串中的中文:

public class CutChineseFromStr {

        public static void main(String[] args) {

                String reStr=CutChinese("234實時234將來a,.bnn.dds看df看");

                System.out.println("截取的中文爲:"+reStr);

        }

        public static String CutChinese(String str){

                //獲取系統編碼

                String encoding=System.getProperty("file.encoding");

                StringBuilder result=new StringBuilder();

                char[] ch=str.toCharArray();

                for(char c:ch){

                        if(encoding.equals("UTF-8")){

                                //系統編碼爲UTF-8時一箇中文佔三個字節

                                if(String.valueOf(c).getBytes().length==3){

                                        result.append(c);

                                }

                        }

                        if(encoding.equals("GBK")){

                                //系統編碼爲GBK時一箇中文佔三個字節

                                if(String.valueOf(c).getBytes().length==2){

                                        result.append(c);

                                }
                        }
            }
                    return result.toString();
        }

}

結果爲:截取的中文爲:實時將來看看

3.統計某個字符在字符串出現的個數:

public class CountSpecifyChar {

        public static void main(String[] args) {

                int count=countChar("sfdwerwerhret",'e');

                System.out.println("字符出現的次數爲:"+count);

        }

        public static int countChar(String str,char c){

                int count=0;

                char[] arr=str.toCharArray();

                for(char ch:arr){

                        if(ch==c){

                                ++count;

                        }

                }

               return count;
        }

}

結果爲:字符出現的次數爲:3

4.判斷一個字符串中子字符串出現的次數:

public class StringContainsSonString {

        public static void main(String[] args) {

                int result=getCount("wersdsfdswersdfsfwerrsdfwer","wer");

                 System.out.println("字符串出現的次數爲"+result);

         }

         public static int getCount(String str,String token){

                int count=0;

                while(str.indexOf(token)!=-1){

                        count++;

                        str=str.substring(str.indexOf(token)+token.length());

                  }

                return count;

        }

}

結果爲:字符串出現的次數爲:4

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