導航:首頁 > 編程語言 > 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字元串中出現的次數相關的資料

熱點內容
怎麼查伺服器假死原因日誌在哪看 瀏覽:277
掃描pdf文件 瀏覽:926
解壓密碼百度雲在線解壓 瀏覽:767
傳播學演算法推薦 瀏覽:749
我的世界網路游戲如何查找伺服器 瀏覽:257
安卓和蘋果通訊錄怎麼互傳 瀏覽:203
怎麼打開隱私與應用加密的菜單 瀏覽:416
我的世界伺服器小游戲的地址大全 瀏覽:578
在網路安全中加密安全機制提供了數據的 瀏覽:249
南京前端程序員私活怎麼收費 瀏覽:981
拓撲pdf 瀏覽:440
如何在工行app查我的訂單 瀏覽:214
車壓縮機改電動 瀏覽:83
如何尋找音樂app 瀏覽:831
一加加密的照片 瀏覽:200
阿里雲虛擬主機php 瀏覽:639
不卡點的解壓視頻 瀏覽:391
hex文件下載單片機 瀏覽:873
實現編譯器的自展技術 瀏覽:655
app開發者怎麼突破 瀏覽:418