日期:2014-05-20  浏览次数:20704 次

JAVA字符串提取字符的问题
比如说的字符串是String   str= "ew22132=--=\[]; ';     wq21   ewq   2 ";
怎么提取上面的每一个字符,不要空格和相同的,然后放在另一个数组或其他存储结构上


------解决方案--------------------
package Jan;

import java.util.Iterator;
import java.util.TreeSet;

public class Getchar {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
TreeSet s = new TreeSet();
String str= "ew22132=--= [] wq21 ewq 2 ";
for(int i =0;i <str.length();i++){
char c = str.charAt(i);
s.add(c+ " ");

}
Iterator it = s.iterator();
while(it.hasNext()){
System.out.println(it.next());
}

}

}

------解决方案--------------------
String str= "ew22132=--=\\[]; '; wq21 ewq 2 ";
List <String> list = new ArrayList <String> ();
for (int i = 0; i < str.length(); i ++){
char c = str.charAt(i);
if(c != ' ' && !list.contains(Character.toString(c))){
list.add(Character.toString(c));
}
}
String[] arr = (String[])list.toArray(new String[list.size()]);
for (String s : arr){
System.out.println (s);
}
------解决方案--------------------
str= "ew22132=--=\\[]; '; wq21 ewq 2 ";
Set resultStr = new HashSet();
char[] toCharArray = str.toCharArray();
for (int i = 0; i < toCharArray.length; i++) {
if(toCharArray[i] != ' ')
resultStr.add(String.valueOf(toCharArray[i]));
}

String[] result= (String[])resultStr.toArray();
for (int i = 0; i < result.length; i++) {
System.out.print(result[i]);
}