字符串去除重複值

        public static void main(String[] args) {
		String aaa = "47310;11111;2222;5555;2222;47310";
		try {
			String bbb = removeDuplicate(aaa, ";");
			System.err.println(bbb);
		} catch (Exception e) {
		}
	}
	/**
	 * Purpose:去除重复值
	 * @author Hermanwang
	 * @param fromString:原字符串
	 * @param splitString:分隔符
	 * @throws Exception
	 * @return String
	 */
	public static String removeDuplicate(String fromString, String splitString) throws Exception {
		String string = null;
		try {
			if (StringUtils.hasText(fromString) && StringUtils.hasText(splitString)) {
				String[] array = fromString.split(splitString);
				if (array.length > 0) {
					List<String> list = new ArrayList<String>();
					list.add(array[0]);
					for (int i = 1; i < array.length; i++) {
						//如果不包含該值,則add到list中
						if (!list.contains(array[i])) {
							list.add(array[i]);
						}
					}
					//[47310,11111,2222,5555]
					string = list.toString();
					//47310,11111,2222,5555
					string = org.apache.commons.lang.StringUtils.strip(list.toString(),"[]");
					//47310;11111;2222;5555
					string = string.replaceAll(", ", splitString);
				}
			} else {
				return fromString;
			}
			return string;
		} catch (Exception e) {
			return fromString;
		}
	}
阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页