字符串去除重複值

        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;
		}
	}
阅读更多

linq 去除重複項死活不行

02-19

rn [code=vb] Dim o As New DataTablern o = OLDMJ.DataSourcern Dim n As New DataTablern n = NEWMJ.DataSourcern Dim vDRnull As DataRow = n.Rows.Addrn Dim leftdata = From oldMJs In o Group Join newMJs In n On oldMJs("ZL") Equals newMJs("ZL") Into Group Let G = If(Group.FirstOrDefault Is Nothing, vDRnull, Group.FirstOrDefault)rn Select New With .ZL = oldMJs("ZL"), _rn .oldMJ = oldMJs("MJ"), _rn .oldML = oldMJs("ml"), _rn .newMJ = G("MJ"), _rn .NEWML = G("ml")rn Dim vdnull As DataRow = o.Rows.Addrn Dim rightdata = From newmj In n Group Join oldtmj In o On newmj("ZL") Equals oldtmj("ZL") Into Group Let b = If(Group.FirstOrDefault Is Nothing, vdnull, Group.FirstOrDefault)rn Select New With .ZL = newmj("ZL"), _rn .oldMJ = b("MJ"), _rn .oldML = b("ml"), _rn .newMJ = newmj("MJ"), _rn .NEWML = newmj("ml")rn Dim aks = rightdata.AsEnumerable.Union(leftdata).CopyToDataTable '.Distinct()rn Dim jk = (From ak In aks Select New With _rn .ZL = ak("ZL"), _rn .newMJ = ak("newMJ"), _rn .newML = ak("NEWML"), _rn .oldMJ = ak("oldMJ"), _rn .oldML = ak("oldML")) [/code]rn結果成這樣了中間還出現了個分割空白,這是怎麼回事用Distinct()也不行老樣子rn [code=text]11 21355.16 941225.82 22314.23 816552.90rn 08 5357.00 1125986.12 4742.00 638780.93rn 01 43894.04 1099675.87 33210.10 808405.76rn 20 11929.86 355417.70 11405.00 260202.66rn 13 52.00 15088.63 52.00 17969.27rn 03 23716.00 1035490.06 21823.00 731774.87rn 07 25673.87 779286.42 24321.77 787685.82rn 09 2574.00 180685.13 3847.00 163668.39rn 24 1484.00 14995.32 1301.00 13336.89rn 23 1357.00 22786.07 1399.00 19230.71rn 15 5181.00 282145.40 6541.65 191340.64rn 04 8022.42 266645.47 7648.16 257231.28rn 14 2360.00 129164.64 2360.00 104889.31rn 41 6912.00 106418.14 6912.00 133493.39rn 05 614.00 39913.02 1617.35 27634.54rn 02 7971.00 404396.23 10993.00 383445.53rn 06 1474.00 90243.27 2345.00 82909.70rn 32 2985.00 147905.82 1939.00 97435.21rn 10 16727.00 582638.43 21749.00 480536.74rn 12 1453.00 116433.44 1493.87 85583.27rn 26 43851.00 745790.42 45182.55 450968.87rn 39 1348.00 32340.18 1617.00 18908.99rn 17 6439.00 130699.34 10617.65 136772.71rn 28 86.00 663.25 86.00 477.34rn 21 657.00 43003.62 1163.00 37360.57rn 60 0.00 0.00 0.00 0.00rn 37 391.00 6087.13 391.00 14357.77rn 40 725.00 5027.01 940.00 19513.56rn rn 11 21355.16 941225.82 22314.23 816552.90rn 08 5357.00 1125986.12 4742.00 638780.93rn 01 43894.04 1099675.87 33210.10 808405.76rn 07 25673.87 779286.42 24321.77 787685.82rn 32 2985.00 147905.82 1939.00 97435.21rn 09 2574.00 180685.13 3847.00 163668.39rn 37 391.00 6087.13 391.00 14357.77rn 03 23716.00 1035490.06 21823.00 731774.87rn 20 11929.86 355417.70 11405.00 260202.66rn 04 8022.42 266645.47 7648.16 257231.28rn 26 43851.00 745790.42 45182.55 450968.87rn 40 725.00 5027.01 940.00 19513.56rn 12 1453.00 116433.44 1493.87 85583.27rn 15 5181.00 282145.40 6541.65 191340.64rn 10 16727.00 582638.43 21749.00 480536.74rn 13 52.00 15088.63 52.00 17969.27rn 24 1484.00 14995.32 1301.00 13336.89rn 23 1357.00 22786.07 1399.00 19230.71rn 14 2360.00 129164.64 2360.00 104889.31rn 41 6912.00 106418.14 6912.00 133493.39rn 05 614.00 39913.02 1617.35 27634.54rn 02 7971.00 404396.23 10993.00 383445.53rn 06 1474.00 90243.27 2345.00 82909.70rn 17 6439.00 130699.34 10617.65 136772.71rn 39 1348.00 32340.18 1617.00 18908.99rn 21 657.00 43003.62 1163.00 37360.57rn 45 1455.00 10491.60rn 85 0.00 5700.85rn 28 86.00 663.25 86.00 477.34rn 60 0.00 0.00 0.00 0.00rn [/code]rn rn

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