java怎么实现统计一个字符串中字符出现的次数?

java怎么实现统计一个字符串中字符出现的次数~

//找出li的数量
String str="lidfdligfdhlidfgdlifglivlifglihglidli";

String targ="li";
int lilength=targ.length();//获取lid 长度
int index = 0;
int count = 0;
index = str.indexOf("li");//获得第一个索引找不到返回-1
while(index != -1){//如果索引不等于-1就继续查找
count ++;//计数累加
index += lilength;//获得迭代后的开始索引号
index = str.indexOf(targ,index);//获得迭代后的某个开始索引号后的第一个li
}
System.out.println(targ+"的数量是:"+count);

哈哈,这个是一种思想,不过也不算复杂的。
比如代码:
String testStr="a1b2c3d4e5";
//这个字符串的长度是10。我们假设就有10个不同的字符。
//用一张2列10行的二维表来描述该情况!这张二维表的第一列表示新出现的字符,对应的第二列表示该字符出现的次数!
主要还是数组遍历查找,得到所有字符(不重复)放到一个2维的存储结构,然后继续统计这些字符的出现次数。

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;

}

}

/**
* @param selectStr 查询字符
* @param targetStr 目标字符串
* @return
*/
public static int findStrIndexOfCount(String selectStr, String targetStr) {
int selectLength = selectStr.length();
int targetLength = targetStr.length();
if (selectLength > 0 && selectLength < targetLength) {
return (targetLength - targetStr.replaceAll(selectStr, "").length()) / selectLength;
}
return -1;
}

public static void main(String[] args) {
System.out.println(findStrIndexOfCount("w", "wawawow"));
}

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的字符范围很广的,你的意思是不是指 字母啊?
字母的话就好做多了,数组+if+for,搞定!多想想,实在不懂再问!

题意不完整呀,你这么说出来能有好多做法!

JAVA编写:输入一个字符串然后统计某字符的个数输出
答:就是用一个for循环,遍历字符串,如果发现那个字符,个数就加一。int count = 0;//个数 char c='a';String s = "abdelabdbals";for(int i=0;i<string.length();i++){ if(s.charAt(i)==c) count++;} System.out.println(count);...

java中Scanner键盘输入一个字符串 要求计算出每个字符出现的次数_百 ...
答:(1)利用键盘录入,输入一个字符串 (2)统计该字符串中各个字符的数量 / public class InputString { public static void main(String[] args) { //获取键盘输入的字符串 Scanner sc=new Scanner(System.in);System.out.println("请输入一个字符串!");String st=sc.nextLine();//将字符串存分...

编写一个Java程序,用于接受一个字符串,并统计某字符在该字符串中出现...
答:代码如下:public class Test { public static void main(String[] args) { String str="abcabcabc";char c= 'a';int times=0;for(int i=0;i<str.length();i++){ if(str.charAt(i)==c)times++;} System.out.println(c+"在字符串"+str+"中共出现:" +times+"次");} } ...

java怎么实现统计一个字符串中字符出现的次数?
答:我办法很简单,拆分存放数组里面,然后查找数组就可以了!其实还有更简单的办法,直接用容器比较方便,目前这个办法我自己用数组写的,我个人挺满意的!import java.util.Scanner;public class 统计次数 {private static Scanner sc=new Scanner(System.in);public static void main(String[] args) {System.out...

用java实现:键盘输入一行字符,分别统计出其中英文字母、空格、数字和...
答:String str=br.readLine();int countNum = 0;//统计数字的个数 int countChar = 0;//统计英文字母的个数 int countSpace = 0;//统计空格的个数 int countOthers = 0;//统计其它字符的个数 for (int i = 0; i < str.length(); i++) { char c = str.charAt(i);if (c >= '...

java中如何统计一个字符串的长度
答:首先打开eclipse 新建一个java项目,名字随意起 名字起好后,点击完成 右键点击项目名称,新建,类 类的名字叫TextLength 包的名字叫 com.zf.s2 点击完成 首先要判断是否是汉字 public static int getChineseCount(String s) throws Exception{//获得汉字的长度 char c;int chineseCount=0;if(!""....

java中 如何统计一段字符串中相同字符的个数
答:通过循环遍历字符串,然后一个个的比较,记下相同字符的个数就行了。代码如下:import java.util.Scanner;import java.util.TreeMap;/** * 从键盘输入16位长整数,编程统计每个数字出现的个数 * @author young * */public class CharMapDemo {// 统计数字或者字符出现的次数public static TreeMap<...

java怎么实现统计一个字符串中字符出现的次数
答:可以用String的indexof(str,fromindex)方法,循环遍历加一个计数器统计次数。public class CountTimes {public static void main(String[] args) {String str="In the entire world there's nobody like me and my best wishes is become myself";int times = searchstr("my", str); //返回2...

java怎么查询一个字符串中重复的字符,并且计算他出现的次数。
答:/ public static void main(String[] args) { // TODO Auto-generated method stub String str = "abcdabcdabcdabcd";String subStr = "abcd";int cursor = 0, subStrLen = subStr.length(), totalStrLen = str.length();int count = 0; // 表示重复字符的个数 while ((cursor + sub...

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> get...

IT评价网,数码产品家用电器电子设备等点评来自于网友使用感受交流,不对其内容作任何保证

联系反馈
Copyright© IT评价网