導航:首頁 > 編程語言 > java統計字元串中每個字元出現的次數

java統計字元串中每個字元出現的次數

發布時間:2024-12-09 14:02:27

java中怎麼統計一個字元串中每個字元的出現次數

操作如下:

String str ="2342asfghgyu56asdasda";Map<String,Integer> maps = new HashMap<String,Integer>();for(int i=0;i<str.length();i++){。

String key = String.valueOf((str.charAt(i)));if(!maps.containsKey(key)),maps.put(key, 1);else{int val =maps.get(key);maps.put(key, val+1);

for(Map.Entry i : maps.entrySet()){System.out.println(i.getKey()+ "=="+i.getValue());

Ⅱ 用java編寫一個函數,統計一個字元串中每個字母出現的次數,謝謝啦

Stringstr="123eeerfdfr5656$%";
HashMap<Character,Integer>hm=newHashMap<Character,Integer>();
char[]arr=str.toCharArray();
for(charc:arr){
hm.put(c,hm.containsKey(c)?hm.get(c)+1:1);
}
System.out.println(hm);

Ⅲ java怎麼實現統計一個字元串中字元出現的次數

可以用String的indexof(str,fromindex)方法,循環遍歷加一個計數器統計次數。

publicclassCountTimes{

publicstaticvoidmain(String[]args){

Stringstr="Intheentireworldthere'";

inttimes=searchstr("my",str);//返回2

System.out.println(times);

}

publicstaticintsearchstr(Stringkey,Stringstr){

intindex=0;//每次的搜索到的下標

intcount=0;//計數器
while((index=str.indexOf(key,index))!=-1){

index=index+key.length();

count++;
}
returncount;

}

}

Ⅳ java 統計字元串中指定字元出現的次數

已經修改好,如下:
public static void main(String[] args) throws IOException{

byte str[]=new byte[20];
byte bt[]=new byte[2];
System.out.println("請輸入一組字元...");
System.in.read(str) ;
System.out.println("請輸入要統計的字元...");
System.in.read(bt);
int i;
int count=0;
System.out.println(str.length);
for(i=0;i<=str.length;i++){
if(str[i]!=0)
{
if(bt[0]==str[i])
{
count++;
System.out.println(str[i]);
}

}
else break;
}
System.out.println("字元"+bt+"在字元串中出現的次數為:"+count);
}

Ⅳ java怎麼實現統計一個字元串中字元出現的次數

說下思路,字元串轉成
字元數組
,然後遍歷。結果存Map<Character,Integer>。如果需要代碼,追問吧,現在是手機回答的。

Ⅵ java 統計一個字元串中某字元出現的次數

public class CharCounter{
public static int counter(String s,char c){
int count=0;
for(int i=0;i<s.length();i++){
if(s.charAt(i)==c){
count++;
}
}
return count;
}
public static void main(String args[]){
System.out.println(new CharCounter().counter("LOVELOVEYOU",'O'));
}
}
試試這個,調試好了,可以直接運行,祝工作學習順利

閱讀全文

與java統計字元串中每個字元出現的次數相關的資料

熱點內容
cs6的魔棒工具 瀏覽:243
文件夾變成受系統保護 瀏覽:479
雨林木風系統安裝教程win7 瀏覽:886
黨中央文件有哪些 瀏覽:872
關於頁眉頁腳文件圖片 瀏覽:147
editbin文件 瀏覽:199
手機怎麼編寫文件到qq 瀏覽:483
蘋果5怎麼樣清理內存 瀏覽:870
win7屏保文件 瀏覽:784
agk什麼文件 瀏覽:441
安卓app編程用什麼 瀏覽:222
文件下載路徑設置不可用 瀏覽:192
win7打不開本地視頻文件 瀏覽:740
怎麼解壓targz文件 瀏覽:358
大數據系統的預算一般多少 瀏覽:381
光陽智能網怎麼下載App 瀏覽:868
編程與c語言哪個好學 瀏覽:624
智能手機如何恢復剪貼板數據 瀏覽:470
直播號是什麼app 瀏覽:333
cisco查看配置文件 瀏覽:414

友情鏈接