补充一些String的例子。
1.剔除重复的字符
剔除ASCII码构成的字符串str中重复的字符,如输入"25522aaddadee25dsa",输出为字符串"25ades"。使用一个 boolean[] map记录字符串str中拥有的字符。
public static String delRepeat(String str){
char[] input = str.toCharArray();
for(char x:input ){
if(x>255)throw new IllegalArgumentException();
}
boolean[] map = new boolean[255];
int index;
for(int i = 0;i< input.length;i++){
index = input[i];
//if(!map[index])
map[index] = true;
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < map.length; i++) {
if(map[i] )
sb.append((char)i);
}
return sb.toString();
}
也可以使用BitSet,见algorithm.bitOp.BitSetDemo。
2.汉字问题
String s = "yqj啊2065";
char[] contents = s.toCharArray(); //y(121) 啊(21834) 2(50) 0(48) 个(20010) 6(54) 5(53) Char[] length = 7
public static void getUnicode(String s){
//String s = "yqj啊2065";
pln("String =\""+ s +"\",length = "+ s.length());
pln("====================== byte array =====================");
String[] charsetNames = new String[]{
java.nio.charset.Charset.defaultCharset().name(),
"UTF-8",
"GB2312",
"GBK",
"ISO8859-1"
};
boolean printed =false ;//默认字符集只想打印一次
for(String x : charsetNames){
byte[] bs = null;//String的byte[]表示
try{
bs = s.getBytes(x);
if( x.equals(charsetNames[0]) ){
if( !printed ){
printed = true;
}else continue;
}
pln("by "+x +",byte[].len = "+ bs.length +",unicode is:" );
for(byte b : bs){
p(b+" ");
}
pln();
pln("new String(bs)→\""+new String(bs)+"\"");
pln("new String(bs,"+x+") →\""+new String(bs,x)+"\"");
}catch (java.io.UnsupportedEncodingException e) { }
pln();
}
}
String ="y啊20个65",length = 7
====================== byte array =====================
by UTF-8,byte[].len = 11,unicode is:
121 -27 -107 -118 50 48 -28 -72 -86 54 53
new String(bs)→"y啊20个65"
new String(bs,UTF-8) →"y啊20个65"
by GB2312,byte[].len = 9,unicode is:
121 -80 -95 50 48 -72 -10 54 53
new String(bs)→"y��20��65"
new String(bs,GB2312) →"y啊20个65"
by GBK,byte[].len = 9,unicode is:
121 -80 -95 50 48 -72 -10 54 53
new String(bs)→"y��20��65"
new String(bs,GBK) →"y啊20个65"
by ISO8859-1,byte[].len = 7,unicode is:
121 63 50 48 63 54 53
new String(bs)→"y?20?65"
new String(bs,ISO8859-1) →"y?20?65"
传说中的63.
3