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

java字元串中出現的次數

發布時間:2022-07-18 20:54:37

1. java中如何統計某個字母在一個字元串中出現了多少次

1、根據length獲取字元串長度

1
2

String s = "abcdedfae";//定義一個字元串
int len = s.length();//獲取原來的字元串長度

2、通過replaceAll方式,把字元串中該字母替換成空

1

String s1 = s.replaceAll(要統計的字母,"");

3、獲取替換後的字元串長度

1

int len2 = s1.length();

4、原來的字元串長度減去替換後的字元串長度就是該字母出現的次數

1

int lenTimes = len1-len2;//出現的次數

2. java怎麼計算一個字元在字元串中出現的次數

import java.util.*;
public class Test {
public static void main(String args[]){
String s = "abcdad"; //待測試的字元串
Map<Character, Integer> result = getCharMaps(s);
System.out.println(result);//列印出字元串中各字元出現的次數!
}
public static Map<Character, Integer> getCharMaps(String s) {
Map<Character, Integer> map = new HashMap<Character, Integer>();
for(int i = 0; i < s.length(); i++) {
Character c = s.charAt(i);
Integer count = map.get(c);
map.put(c, count == null ? 1 : count + 1);
}
return map;
}
}

3. Java程序如何統計某字元在該字元串中出現的次數思路是什麼

思路:
先輸入字元串與想要統計的字元,然後遍歷字元串的每一個字元,進行統計。
代碼:
import java.text.ParseException;
import java.util.Scanner;

class DemoAsm{
public static void main(String[] args) throws ParseException {
Scanner sc =new Scanner(System.in);
//輸入字元串
String str=sc.nextLine();
//輸入字元
String str1=sc.nextLine();

//遍歷字元串
int sum=0;
for(int i=0;i<str.length();i++){
if(str.substring(i, i+1).equals(str1)){
sum++;//統計
}
}
System.out.println(str1+"出現了"+sum+"次");
}
}

4. java,找出指定字元在字元串中出現的次數

//你的代碼寫的一踏糊塗 我重新幫你寫了段 希望你能採納 不懂可以問

// 入口類
public class p1
{
public static void main(String[] args)
{
p2 p=new p2();
p.jj();
}
}
//邏輯代碼類
public class p2
{
int a=0;

public void jj()
{
Tools t=new Tools();
t.output("請輸入一個字元串:");
String name=t.inputString();
t.output("請輸入要查找的字元:");
String hg=t.inputString();
int cishu=gg(name,hg);
t.output("「"+name+"」"+"中包含"+cishu+"個"+hg);

}
public int gg(String name,String hg)
{
for(;;)
{
int kl=name.indexOf(hg);
if(kl>=0)
{
kl++;
name=name.substring(kl);
a++;
}
else
{
break;
}
}
return a;
}

}
//工具類 好比scanner 類
import java.util.*;
public class Tools
{
public String inputString()
{
Scanner scanner = new Scanner(System.in);
String s= scanner.next();
return s;
}
public int inputInt()
{
Scanner scanner = new Scanner(System.in);
int s= scanner.nextInt();
return s;
}
public double inputDouble()
{
Scanner scanner = new Scanner(System.in);
double s= scanner.nextDouble();
return s;
}
public void output(String s)
{
System.out.print(s);
}
}

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

import java.util.Scanner;
import java.util.Set;
import java.util.TreeMap;
/*
* 需求 :"aababcabcdabcde",獲取字元串中每一個字母出現的次數要求結果:a(5)b(4)c(3)d(2)e(1)
*
* 分析:
* A:定義一個字元串(可以改進為鍵盤錄入)
* B:定義一個TreeMap集合
* 鍵:Character
* 值:Integer
* C:把字元串轉換為字元數組
* D:遍歷字元數組,得到每一個字元
* E:拿剛才得到的字元作為鍵到集合中去找值,看返回值
* 是null:說明該鍵不存在,就把該字元作為鍵,1作為值存儲
* 不是null:說明該鍵存在,就把值加1,然後重寫存儲該鍵和值
* F:定義字元串緩沖區變數
* G:遍歷集合,得到鍵和值,進行按照要求拼接
* H:把字元串緩沖區轉換為字元串輸出
*
* 錄入:linqingxia
* 結果:result:a(1)g(1)i(3)l(1)n(2)q(1)x(1)
*/
public class TreeMapDemo {
public static void main(String[] args) {
// 定義一個字元串(可以改進為鍵盤錄入)
Scanner sc = new Scanner(System.in);
System.out.println("請輸入一個字元串:");
String line = sc.nextLine();
// 定義一個TreeMap集合
TreeMap<Character, Integer> tm = new TreeMap<Character, Integer>();
//把字元串轉換為字元數組
char[] chs = line.toCharArray();
//遍歷字元數組,得到每一個字元
for(char ch : chs){
//拿剛才得到的字元作為鍵到集合中去找值,看返回值
Integer i = tm.get(ch);
//是null:說明該鍵不存在,就把該字元作為鍵,1作為值存儲
if(i == null){
tm.put(ch, 1);
}else {
//不是null:說明該鍵存在,就把值加1,然後重寫存儲該鍵和值
i++;
tm.put(ch,i);
}
}
//定義字元串緩沖區變數
StringBuilder sb= new StringBuilder();
//遍歷集合,得到鍵和值,進行按照要求拼接
Set<Character> set = tm.keySet();
for(Character key : set){
Integer value = tm.get(key);
sb.append(key).append("(").append(value).append(")");
}
//把字元串緩沖區轉換為字元串輸出
String result = sb.toString();
System.out.println("result:"+result);
}
}
/***不懂裡面的的一些方法的可以找本書看看Map集合方面的,還有學會查API,否則你一輩子都讀不懂JAVA程序的,其實我這個不用分析的話應該是這個問題的最簡解了吧。。。。***/

6. 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;

}

}

7. java中如何統計某個字母在一個字元串中出現了多少次啊

1、根據length獲取字元串長度

Strings="abcdedfae";//定義一個字元串
intlen=s.length();//獲取原來的字元串長度

2、通過replaceAll方式,把字元串中該字母替換成空

Strings1=s.replaceAll(要統計的字母,"");

3、獲取替換後的字元串長度

intlen2=s1.length();

4、原來的字元串長度減去替換後的字元串長度就是該字母出現的次數

intlenTimes=len1-len2;//出現的次數
閱讀全文

與java字元串中出現的次數相關的資料

熱點內容
麗水四軸加工中心編程 瀏覽:675
國產系統怎麼解壓 瀏覽:552
戰雙程序員 瀏覽:483
him觸摸編程軟體 瀏覽:931
植物大戰僵屍存檔怎麼轉移安卓 瀏覽:852
java棧的元素 瀏覽:737
程序員與籃球事件 瀏覽:675
app反編譯不完整 瀏覽:788
電腦上的文件夾怎麼調整 瀏覽:7
伺服器無響應是什麼原因呀 瀏覽:984
wd文檔里的app怎麼製作 瀏覽:513
電腦里的文件夾沒有了一般能恢復嗎 瀏覽:418
哪裡有配加密鑰匙的 瀏覽:210
伺服器開不了機怎麼把數據弄出來 瀏覽:958
gif動態圖片怎麼壓縮 瀏覽:521
黑猴子棒球壓縮文件解壓密碼 瀏覽:631
如何讓app適應不同的手機屏幕大小 瀏覽:10
蘋果手機如何給安卓手機分享軟體 瀏覽:761
蘋果電腦怎麼運行騰訊雲伺服器 瀏覽:59
明日之後沙石堡命令助手 瀏覽:261