字符串去重五种方法

public static void main(String[] args) {String str =getStr("aweswedwerwe");//System.out.println(str);}//to string方法public static String toString(List list){//接收list集合String result="";//遍历数组for(Object o: list){//类型转换String w =(String) o;//强制类型转换//拼接result=result+w;}return result;}	public static String getStr(String w){String result="";ArrayList list=new ArrayList();//for循环-取出每一个子串//存储到list集合中,存之前先判断一下有没有for (int i = 0; i < w.length(); i++) {String str=	w.charAt(i)+"";//!取反 有不存 无存if(!list.contains(str)){list.add(str);}} result= toString(list);System.out.println(result);return result;}
public static void main(String[] args) {String str =getStr("abcabcabc");}
public static String toString(Map map){String result="";for (Object o : map.keySet()) {String w=(String) o;result=result+w;}return result;}public static String getStr(String w){String result="";
Map map=new HashMap();for (int i = 0; i < w.length(); i++) {String str=w.charAt(i)+"";map.put(str,null);//if(!map.containsKey(str)){//map.keySet();//	}}System.out.println(map);System.out.println(toString(map));return result;}
public static void main(String[] args) {String str =getStr("apowpowpoepo");
}
//to string方法
public static String tosString(Set set){
String	result="";for (Object o : set) {String w=(String) o;result=result+w;}return result;}public static String getStr(String w){String result="";Set set =new HashSet();//遍历字符串for (int i = 0; i < w.length(); i++) {String str=w.charAt(i)+"";//取出的字符转换成字符串set.add(str);}result=tosString(set);System.out.println(result);return result;}
public static void main(String[] args) {String str = getStr("awedwefwegwe");System.out.println(str);}public static String getStr(String s){String result="";//遍历字符串//遍历每一个子串 拼接到result中//拼接之前判断是否 已经存在了这个子串 ,如果存在 就不拼接 不存在得话就拼接 for(int i=0;i<s.length();i++){String str = s.charAt(i)+"";//取出字符串当中的每一个子串if(result.indexOf(str)==-1){//result不存在的子串result=result+str;//拼接}}return result;}

Published by

风君子

独自遨游何稽首 揭天掀地慰生平