日期:2014-05-20 浏览次数:20838 次
public static void main(String[] args) throws UnknownHostException, IOException { String str1 = "新能源汽车,整车,我的,傻×"; String str2 = "新能源汽车,整车,天天,谔谔,我的"; for(String str:str1.split(",")) { System.out.println(Arrays.asList(str2.split(",")).contains(str)?str:""); } }
------解决方案--------------------
public static void main(String[] args) { String str1 = "新能源汽车,整车,我的,傻×"; String str2 = "新能源汽车,整车,天天,谔谔,我的"; List<String> l1 = new ArrayList<String>(Arrays.asList(str1.split(","))); l1.retainAll(new ArrayList<String>(Arrays.asList(str2.split(",")))); System.out.println(l1); }
------解决方案--------------------
纯新手,刚学习了集合,写个试试,楼主不要笑话啊
import java.util.*; public class Stastic { public static void main(String[] args) { String str1 = "新能源汽车,整车,我的,傻×"; String str2 = "新能源汽车,整车,天天,谔谔,我的"; int totalCount=str1.split(",").length+str2.split(",").length; Set<String> set=new HashSet<String>(Arrays.asList(str1.split(","))); Collections.addAll(set,str2.split(",")); System.out.println("the repeat string number is "+(totalCount-set.size())); } }