String 去重,区分大小写

题目要求:去除,和.,相同的单词去除后面的。区分大小写数据结构

示例:输入: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用正则匹配,去掉,和.
相关文章
相关标签/搜索