Ⅰ java怎麼判斷一個字元串中字母出現的次數
1、根據length獲取字元串長度
1
2
String s = "abcdedfae";//定義一個字元串
int len = s.length();//獲取原來的字元串長度
2、通過replaceAll方式,把字元串中該字母替換成空
1
String <a href="https://www..com/s?wd=s1&tn=44039180_cpr&fenlei=--bIi4WUvYETgN-TLwGUv3EPH6YPWDsrjD3" target="_blank" class="-highlight">s1</a> = s.replaceAll(要統計的字母,"");
3、獲取替換後的字元串長度
1
int len2 = <a href="https://www..com/s?wd=s1&tn=44039180_cpr&fenlei=--bIi4WUvYETgN-TLwGUv3EPH6YPWDsrjD3" target="_blank" class="-highlight">s1</a>.length();
4、原來的字元串長度減去替換後的字元串長度就是該字母出現的次數
1
int lenTimes = len1-len2;//出現的次數
Ⅱ 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 怎樣從一個string字元串中判斷某個字母出現的次數
import java.util.Scanner;
public class test{
public static void main(String[] args) {
/*
判斷是否是字母,將輸入的字元串一個個取出,然後轉換成char型,
然後再將char型強制轉換成int型,這時候返回的是一個ASCII碼,
ASCII碼在97到122之間是小寫字母,ASCII碼在64到90之間是大寫字母。
* */
int count = 0;
System.out.print("請輸入一個字元串:");
String str = new Scanner(System.in).next();
//輸入字元串後判斷是不是含英文字母的字元串
for (int i = 0; i < str.length(); i++) {
if (((int) str.substring(i, i + 1).charAt(0) >= 97 && (int) str
.substring(i, i + 1).charAt(0) <= 122)
|| ((int) str.substring(i, i + 1).charAt(0) >= 64 && (int) str
.substring(i, i + 1).charAt(0) <= 90)) {
count++;
}
}
while (1 == 1) {
//當count大於等於1的時候說明這個字元串中,含有字母
if (count>=1) {
break;
}else{
//如果不是含字母的字元串,就繼續循環,直到輸入含字母的字元串
System.out.println("你輸入的不是含字母的字元串!");
System.out.println(" ");
System.out.print("請輸入一個字元串:");
str = new Scanner(System.in).next();
for (int i = 0; i < str.length(); i++) {
if (((int) str.substring(i, i + 1).charAt(0) >= 97 && (int) str
.substring(i, i + 1).charAt(0) <= 122)
|| ((int) str.substring(i, i + 1).charAt(0) >= 64 && (int) str
.substring(i, i + 1).charAt(0) <= 90)) {
count++;
}
}
}
}
//後面還要使用count統計,所以此處要將count清零
count = 0;
System.out.print("請輸入你要查找的字母:");
String scanf = new Scanner(System.in).next();
//輸入要查找的字元串後判斷是不是只有一個字母,或者是不是一個字母
while (1 == 1) {
if ((((int) scanf.charAt(0) >= 97 && (int) scanf.charAt(0) <= 122) || ((int) scanf
.charAt(0) >= 64 && scanf.charAt(0) <= 90))
&& (scanf.length() == 1)) {
break;
} else {
System.out.println("你輸入的不是一個英文字母!");
System.out.println(" ");
System.out.print("請輸入一個你要查找的字母:");
scanf = new Scanner(System.in).next();
}
}
//統計字母在那個字元串中出現的次數
for (int i = 0; i < str.length(); i++) {
if (str.substring(i, i + 1).equals(scanf)) {
count++;
}
}
if (count == 0) {
System.out.println("字母" + scanf + "不存在字元串:" + str + "中!");
} else {
System.out.println("字母" + scanf + "在字元串:" + str + "中出現了" + count
+ "次!");
}
}
}
Ⅳ java 怎樣從一個string字元串中判斷某個字母出現的次數
可以轉換為char數組,然後用map存起來,鍵存具體的字母,值存出現的次數。看我寫的代碼,我測試過了,可以運行。
importjava.util.HashMap;
importjava.util.Map;
{
publicstaticvoidmain(String[]args){//測試方法
Stringstr="hereissomelettershello";
countTimes(str.toCharArray());
}
publicstaticvoidcountTimes(char[]arr){
Map<Integer,Integer>map=newHashMap<Integer,Integer>();
for(inti=0;i<arr.length;i++){
inttemp=arr[i];
Integerresult=map.get(temp);
if(result==null){
map.put(temp,1);
}else{
map.put(temp,result+1);
}
}
for(inti:map.keySet()){
System.out.println("String中字母"+(char)i+"的出現次數為:"+map.get(i)+"次");
}
}
}
Ⅳ java中如何統計某個字母在一個字元串中出現了多少次啊
1、根據length獲取字元串長度
Strings="abcdedfae";//定義一個字元串
intlen=s.length();//獲取原來的字元串長度
2、通過replaceAll方式,把字元串中該字母替換成空
Strings1=s.replaceAll(要統計的字母,"");
3、獲取替換後的字元串長度
intlen2=s1.length();
4、原來的字元串長度減去替換後的字元串長度就是該字母出現的次數
intlenTimes=len1-len2;//出現的次數
Ⅵ java中怎麼判斷字元出現的次數
很基礎的題目啊,給你個思路自己寫代碼。
定義一個字元串,比如叫queryString,記錄字元串。
定義一個字元,比如叫queryChar,記錄要查找的字元。
定義一個整數,比如叫hits,記錄出現次數。初值為0。
利用Scanner輸入queryString和queryChar。
利用Java中String類提供的方法,求出queryString的長度length。
for (int i = 0; i < length; i++)
{
從queryString中取出第i個字元
如果這個字元和queryChar相等,則hits++
}
輸出hits,結束。
Ⅶ java,怎樣從一個string字元串中判斷某個字母出現的次數
1、使用 String.replace() 把目標字母替換掉,然後算字元串長度的變化。
2、substring()方法,看看出現了幾個數組-1。
Ⅷ 在Java中,輸入一個字元串,再輸入一個字元,判斷該字元在該字元串中出現的次數(使用substring()方法)
importjava.util.Scanner;
publicclassSubstring{
publicvoidSub()
{
inttimes=0;
Stringzfc=newScanner(System.in).next();
Stringzf=newScanner(System.in).next();
for(inti=0;i<zfc.length()-1;i++)
{
Stringsub=zfc.substring(i,i+1);
if(sub.equals(zf))
{
times++;
}
}
System.out.println("出現的次數為:"+times);
}
publicstaticvoidmain(String[]args){
newSubstring().Sub();
}
}
Ⅸ JAVA 判斷一行字元串中某個字串出現的次數要用哪種方法
可以用indexOf(String str)這個方法,返回第一次出現的指定子字元串在此字元串中的索引。然後再把索引前的字元串都截取掉,再用indexOf(String str)方法,再截取,知道最後,中間用個i++,不就知道有多少了。
Ⅹ java判斷字元串中的一個 "字元" 出現了多少次
packagecom.cn.qy.util;
publicclassaa{
publicstaticvoidmain(Stringargs[]){
/*判斷字元ab在字元str中出現的次數*/
//需要對比的源字元串
Stringstr="";
//需要對比的字元串
StringcompareStr="ab";
//字元串查找初始從0開始查找
intindexStart=0;
//獲取查找字元串的長度,這里有個規律:第二次查找出字元串的起始位置等於第一次ab字元串出現的位置+ab的長度
intcompareStrLength=compareStr.length();
intcount=0;
while(true){
inttm=str.indexOf(compareStr,indexStart);
if(tm>=0){
count++;
// 沒查找一次就從新計算下次開始查找的位置
indexStart=tm+compareStrLength;
}else{
//直到沒有匹配結果為止
break;
}
}
System.out.println(count);
}
}