import java.util.ArrayList; import java.util.List; public class Test2 { /** * @param args */ public static void main(String[] args) { String src = "A B C D E B C"; //替换掉空格 src = src.replaceAll(" ", "") ;
System.out.println("去除空格后字符串:" + src) ; List<Character> list = new ArrayList<Character>() ; int[] bb = new int[256]; char[] cs = src.toCharArray();
//倒序 int mid = cs.length / 2 ; int idx = cs.length -1 ; for (int i = 0; i < mid ; i++){ char tmp = cs[i] ; cs[i] = cs[idx] ; cs[idx] = tmp ; idx-- ; } //统计,并且过滤相同的 for (char c : cs) { if (bb[c] <1) { list.add(c) ; } bb[c] = bb[c] + 1; } System.out.println(); for (int i = 0; i < list.size(); i++){ System.out.print(list.get(i)) ; } System.out.println() ;
for (int i = 0; i < list.size(); i++){ char c = list.get(i) ;
System.out.println(c + " " + bb[c] + "次") ; }
} }
String a = "abcd,efg"; String b = ")(*&^%$#@![]{},.//;:'? <>"; 要求是判断String a 里有没有哪个字符在String b里出现 ,效率越高越好