Java中統計字符個數和反序非雷同字符的辦法詳解。本站提示廣大學習愛好者:(Java中統計字符個數和反序非雷同字符的辦法詳解)文章只能為提供參考,不一定能成為您想要的結果。以下是Java中統計字符個數和反序非雷同字符的辦法詳解正文
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裡湧現 ,效力越高越好
* 查找某些字符能否在另外一個字符串裡湧現
*
* @author Java人(java2000.net)
*/
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
String a = "abcd,efg";
String b = ")(*&^%$#@![]{},.///;:'? <>";
byte[] bb = new byte[256];
char[] cs = b.toCharArray();
for (char c : cs) {
bb[c] = 1;
}
cs = a.toCharArray();
for (char c : cs) {
if (bb[c] == 1) {
System.out.println(c);
}
}
}
}