1. 程式人生 > >java面試題:如果一串字符如"aaaabbc中國1512"要分別統計英文字符的數量,中文字符的數量,和數字字符的數量,假設字符中沒有中文字符、英文字符、數字字符之外的其他特殊字符。

java面試題:如果一串字符如"aaaabbc中國1512"要分別統計英文字符的數量,中文字符的數量,和數字字符的數量,假設字符中沒有中文字符、英文字符、數字字符之外的其他特殊字符。

rgs info log letter clas [] 面試題 .com ack

package com.swift;

public class TotalNumber_String {

    public static void main(String[] args) {
        /*
         * 如果一串字符如"aaaabbc中國1512"要分別統計英文字符的數量,中文字符的數量,和數字字符的數量,
         * 假設字符中沒有中文字符、英文字符、數字字符之外的其他特殊字符。
         */
        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(Character.isDigit(ch)) { digitCount++; } else if(String.valueOf(ch).matches("[a-zA-Z]{1}"))//
用isLetter()包含中文字符 { engishCount++; } else { chineseCount++; } } System.out.println("the number of letter is "+engishCount+" ; the number of digit is "+digitCount+" ; the number of chineseCount is "+chineseCount); } }

技術分享圖片

java面試題:如果一串字符如"aaaabbc中國1512"要分別統計英文字符的數量,中文字符的數量,和數字字符的數量,假設字符中沒有中文字符、英文字符、數字字符之外的其他特殊字符。