『壹』 java正則表達式計算文章中漢字個數中一表達式
空串+字元,是為了將字元轉換為字元串類型,這樣就可以使用matches方法了,因為這個方法的第二個參數必須是String類型的。
『貳』 用Java編寫一個程序,輸入一排漢字,找出每個漢字出現的次數,並輸出漢字,與出現次數
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Demo {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String string = scanner.next();
Map<Character, Integer> map = new HashMap<Character, Integer>();
for (int i = 0; i < string.length(); i++) {
Character character = new Character(string.charAt(i));
if (map.get(character) == null) {
map.put(character, 1);
} else {
map.put(character, map.get(character) + 1);
}
}
System.out.println(map.toString());
}
}
基本原理就是循環字元串里的每個字元,將字元作為key,出現次數作為value放到一個map集合里
『叄』 java中如何統計一個字元串的長度
首先打開eclipse
『肆』 用java編程統計用戶從鍵盤輸入的字元串中所包含的字母,數字和其他字元串的個數
importjava.util.Scanner;
publicclassTest{
publicstaticvoidmain(String[]args){
Scannerinput=newScanner(System.in);
System.out.print("輸入字元串:");
Stringstrs=input.next();
intnumber=0;
intchara=0;
intother=0;
char[]chs=strs.toCharArray();
for(charc:chs){
if(c>='0'&&c<='9'){
number++;
}elseif(c>='a'&&c<='z'||c>='A'&&c<='Z'){
chara++;
}else{
other++;
}
}
System.out.println("數字有:"+number+"個,字元有"+chara+"個,其他有:"+other+"個。");
}
}
『伍』 java編程:輸入一個字元串,計算字元串中所包含的字母個數,數字個數,漢字個數!!!
1.接收輸入字元串2.分析字元串中每個字元的ASCII碼,然後進行統計就好了
『陸』 怎樣用java統計漢字字數
根據Character.toString(t1[i]).matches("[\u4E00-\u9FA5]+")來匹配
publicstaticvoidmain(String[]args){
//TODOAuto-generatedmethodstub
Stringsrc="王大內錘容+wefw";
char[]t1=null;
t1=src.toCharArray();
intt0=t1.length;
intcount=0;
for(inti=0;i<t0;i++){
if(Character.toString(t1[i]).matches("[\u4E00-\u9FA5]+")){
count++;
}
}
System.out.println(count);
}
『柒』 java如何去除字元串中的空格並且計算字元串中漢字的個數
去除空格,可以一個一個判斷,是空格則刪除;也可以用split("
"),用空格來分割字元串,然後把分割後的字元串再拼接起來,不過我不確定這種方法在分割後的數組中會不會還有空格。。。
計算漢字個數,好像是用正則表達式匹配,還是編碼值之類的。可能是用正則表達式時,就是利用了編碼值。這個應該可以搜到的。
我當時用的就是這樣方法,沒有找到更好的方法了。