WordFilter { @SuppressWarnings("unchecked") public String getFilterString(final String info, final String replaceTag) { if (info == null || info.length() == 0 || replaceTag == null) { return info; } char[] charArray = info.toCharArray(); int len = charArray.length; StringBuilder newInfo = new StringBuilder(GlobalConstants.BLANK); int i = 0; while (i < len) { int end = -1; int index; Map<Character, Map> sub = filterMap; for (index = i; index < len; index++) { sub = sub.get(charArray[index]); if (sub == null) { if (end == -1) { newInfo.append(charArray[i]); i++; break; } else { for (int j = i; j <= end; j++) { newInfo.append(replaceTag); } i = end + 1; break; } } else { if (sub.containsKey(END_TAG)) { end = index; } } } if (index >= len) { if (end == -1) { newInfo.append(charArray[i]); i++; } else { for (int j = i; j <= end; j++) { newInfo.append(replaceTag); } i = end + 1; } } } return newInfo.toString(); }  @SuppressWarnings("unchecked") void init(final Collection<String> filterWordList); @SuppressWarnings("unchecked") boolean hasFilterWord(final String info); @SuppressWarnings("unchecked") String getFilterString(final String info, final String replaceTag); @SuppressWarnings("unchecked") String getKeyword(final String statement);  }
@Test public void getFilterString() { WordFilter filter = new WordFilter(); Set<String> sss = new HashSet<String>(); sss.add("南京"); sss.add("北京"); filter.init(sss); String str1 = "我家在南京，天安门在北京"; String str2 = filter.getFilterString(str1, "*"); System.out.println(str2); Assert.equals(str2, "我家在**，天安门在**", ErrorCodeDef.SYSTEM_ERROR_10001); System.out.println("敏感字符已经被替换成*号了"); }