題目要求:去除,和.,相同的單詞去除後面的。區分大小寫數據結構
示例:輸入:There is a will,there is a way. 輸出There is a will there wayit
答案代碼:List
String s = "There is a will there is a way";
Pattern p = Pattern.compile("[,.]");
String ss = p.matcher(s).replaceAll("");
LinkedHashSet<String> set = new LinkedHashSet<String>(Arrays.asList(ss.split("[
\\s*\t\r\n]")));
set.forEach(System.out::println);
備註:
1主要用到一種數據結構LinkedHashSet,達到去重不影響順序,區分大小寫的目的。
2用正則匹配,去掉,和.