题目要求:去除,和.,相同的单词去除后面的。区分大小写
示例:输入:There is a will,there is a way. 输出There is a will there way
答案代码:
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* ]")));
set.forEach(System.out::println);
Pattern p = Pattern.compile("[,.]");
String ss = p.matcher(s).replaceAll("");
LinkedHashSet<String> set = new LinkedHashSet<String>(Arrays.asList(ss.split("[\s* ]")));
set.forEach(System.out::println);
备注:
1主要用到一种数据结构LinkedHashSet,达到去重不影响顺序,区分大小写的目的。
2用正则匹配,去掉,和.