有一個字符串,其中包含中文字符、英文字符和數字字符,請統計和打印出各個字符的個數。


如果一串字符如"aaaabbc中國1512"要分別統計英文字符的數量,中文字符的數量,和數字字符的數量,

假設字符中沒有中文字符、英文字符、數字字符之外的其他特殊字符。

package com.aspire.test;

public class Test {
    public static void main(String[] args) {
        
        String str = "aaaabbc中國1512";
        int engishCount = 0;
        int chineseCount = 0;
        int digitCount = 0;
        for(int i=0;i<str.length();i++)
        {
            char ch = str.charAt(i);
            if(ch>='0' && ch<='9')
            {
                digitCount++;
            }
            else if((ch>='a' && ch<='z') || (ch>='A' && ch<='Z'))
            {
                engishCount++;
            }
            else
            {
                chineseCount++;
            }
        }
        System.out.println("....");

    }
}

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM