怎样统计一串字符串字符出现的次数java
3个回答
展开全部
直接上代码
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;
}
}
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;
}
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
String str="jackdjackjack2jackjack";
System.out.println((str+1).split("jack").length-1);
System.out.println((str+1).split("jack").length-1);
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
to char array 遍历
String str = "1122334456789";
char[] chrs = str.toCharArray();
Map<Character, Integer> map = new HashMap<Character, Integer>();
for (char c : chrs) {
if (map.get(c) != null)
map.put(c, map.get(c) + 1);
else
map.put(c, 1);
}
Set<Character> iterator = map.keySet();
for (Character character : iterator) {
System.out.println(character + ":" + map.get(character));
}
String str = "1122334456789";
char[] chrs = str.toCharArray();
Map<Character, Integer> map = new HashMap<Character, Integer>();
for (char c : chrs) {
if (map.get(c) != null)
map.put(c, map.get(c) + 1);
else
map.put(c, 1);
}
Set<Character> iterator = map.keySet();
for (Character character : iterator) {
System.out.println(character + ":" + map.get(character));
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询