代碼如下:
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);
}
}
}
}