java統計出其中英文字母、空格、數字和其它字元的個數的2個例子

來源:互聯網
上載者:User

題目:輸入一行字元,分別統計出其中英文字母、空格、數字和其它字元的個數。

1.程式分析:利用while語句,條件為輸入的字元不為 ‘\n ‘.

import java.util.Scanner;
public class ex7 {
  public static void main(String args[])
  {
   System.out.println("請輸入字串:");
   Scanner scan=new Scanner(System.in);
   String str=scan.next();
   String E1="[\u4e00-\u9fa5]";
   String E2="[a-zA-Z]";
   int countH=0;
   int countE=0;
   char[] arrChar=str.toCharArray();
   String[] arrStr=new String[arrChar.length];
   for (int i=0;i<arrChar.length ;i++ )
   {
    arrStr[i]=String.valueOf(arrChar[i]);
   }
   for (String i: arrStr )
   {
    if (i.matches(E1))
    {
     countH++;
    }
    if (i.matches(E2))
    {
     countE++;
    }
   }
   System.out.println("漢字的個數"+countH);
   System.out.println("字母的個數"+countE);
  }
}


例子2,


分別統計字串的數字、字母、空格和其他字元(包括漢字、標點符號)的個數,並計算及字串的總字元數。
 

package util; 
 
 
public class CountStr { 
    /**
     * 有一個字串,其中包含中文字元、英文字元和數字字元,請統計和列印出各個字元的個數
     * @author Mr.Liao
     * @date 2013-09-04
     * 簡訊發送平台,簡訊字數控制查詢方法
     */ 
        public static void main(String[] args) { 
 
            //String str = "adasf AAADFD我是中文,,》123"; 
            //String str = "金馬甲高端商品交易平台--2013全城熱戀克拉鑽石項目預售,18個月,三萬起步,年化8%,預購請致電展恒私人財富:18611297979"; 
            String str = "展恒理財,2004年在北京成立,是國內最大的理財諮詢類機構之一。獲得國家頒發的獨立基金銷售牌照.是2013年中國網球公開賽10大核心贊助商之一。公司成立10年來,在為客戶進行全面的家庭財務規劃方面積累了十分豐富的經驗。目前擁有中高端忠實客戶10000多名,配置客戶資金超過200億元,位列行業排名前三強。"; 
             
            System.out.println("[總字元數1]:"+countSum(str)); 
            System.out.println("--------------------");              
            System.out.println("[總字元數2]:"+countSum2(str));   
            System.out.println("--------------------");              
            System.out.println("[總字元數3]:"+str.length()); 
        } 
         
        public static int countSum(String str) { 
            int unicodeCount = 0; 
            int szCount = 0; 
            int zmCount = 0; 
 
            for (int i = 0; i < str.length(); i++) { 
 
                char c = str.charAt(i); 
                if (c >= '0' && c <= '9') { 
                    szCount++; 
                }else if((c >= 'a' && c<='z') || (c >= 'A' && c<='Z')){ 
                    zmCount++; 
                }else{ 
                    unicodeCount++; 
                } 
            } 
            System.out.println("Unicode:"+unicodeCount); 
            System.out.println("數字:"+szCount); 
            System.out.println("字母:"+zmCount);           
            int sum=szCount+zmCount+unicodeCount; 
            return sum; 
        }    
        public static int countSum2(String str) { 
            int abccount = 0; 
            int numcount = 0; 
            int spacecount = 0; 
            int othercount = 0; 
            char[] b = str.toCharArray(); 
            for(int i = 0; i < b.length; i++){ 
                if(b[i]>='a'&&b[i]<='z'||b[i]>='A'&&b[i]<='Z'){ 
                    abccount++; 
                }else if(b[i]>='0'&&b[i]<='9'){ 
                    numcount++; 
                }else if(b[i]==' '){ 
                    spacecount++; 
                }else{ 
                    othercount++; 
                } 
        } 
            int sum=abccount+numcount+spacecount+othercount; 
            System.out.println("字串中含有的英文字母數為:" + abccount); 
            System.out.println("字串中含有的數字數為:" + numcount); 
            System.out.println("字串中含有的空格數為:" + spacecount); 
            System.out.println("字串中含有的其他字元為:" + othercount); 
            return sum;  
    } 
} 
 
 
控制台結果:
Unicode:132
數字:20
字母:0
[總字元數1]:152
--------------------
字串中含有的英文字母數為:0
字串中含有的數字數為:20
字串中含有的空格數為:0
字串中含有的其他字元為:132
[總字元數2]:152
--------------------
[總字元數3]:152

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.