- public class Demo4{
- public static void main(String[] args){
- stringCount();
- }
- public static void stringCount(){
- String str="ewqr2345432ewtfaewfads";
- char[] ch=str.toCharArray();
- char[] chs=new char[str.length()];
- int m=0;
- w:for(int i=0;i<str.length();i++){
- for(int j=0;j<chs.length;j++)
- {
- if(chs[j]==ch[i])
- continue w;
- }
- int count=0;
- int index=0;
- while((index=str.indexOf(ch[i],index))!=-1){
- index++;
- count++;
- }
- chs[m++]=ch[i];
-
- System.out.println(ch[i]+":"+count);
- }
-
- }
- }
复制代码
- import java.util.HashMap;
- import java.util.Iterator;
- import java.util.Set;
- public class Demo3 {
- public static void main(String[] args) {
- HashMap<String,String> nums = new HashMap<String,String>();
- String str="ewqr2345432ewtfaewfads";
-
-
- for(int i =0;i<str.length();i++){
- char c = str.charAt(i);
- String s = String.valueOf(c);
- if(nums.containsKey(s)){
- String n = nums.get(s);
- int num = Integer.parseInt(n);
- nums.put(s,String.valueOf(++num));
- }else{
- nums.put(s, "1");
- }
- }
- Set<String> set = nums.keySet();
- Iterator<String> it = set.iterator();
- while(it.hasNext()){
- String key = it.next();
- System.out.println(key+":"+nums.get(key));
- }
-
- }
- }
复制代码 |