繁体   English   中英

从字符串中删除重复项

[英]Remove duplicates from String

这是我的代码,

   public static String set_x_dates()
   {
    int noRecords = GlobalData.getNoRecords();
    int n;
    String date = "";
    if (noRecords <= 10)
        for (n = 0; n < noRecords; n++)
            date += Dates[n] + "-" + Month[n] + "|";
    else {
        for (n = 0; n < noRecords; n++) {
            int gap = (int) (noRecords / 10);
            date += Dates[n] + "-" + Month[n] + "|";
            n++;
            if (n != noRecords)
                for (; gap > 0; gap--)
                    date += "|";
        }

    }
    return date;
}

我想从正在返回的字符串“日期”中删除重复的条目 Dates[] 和 Month[] 是 static int arrays。 有谁能够帮我?

我得到的 output 是这样的:

25-5|28-5|4-6|8-6|10-6|14-6|17-6|7-7|7-7|7-7|7-7|7-7|7-7|7-7|7-7|7-7|7-7|26-7|26-7|

我想要这个:

25-5|28-5|4-6|8-6|10-6|14-6|17-6|7-7|26-7| 

不是将日期连接到字符串,而是在循环记录时将日期添加到Set中。 集合不能包含重复项。

然后在方法结束时,循环你的集合并构造一个要返回的字符串。

您可以组装一字符串,这些字符串将在填充集合后连接起来。

编辑:啊,dogbane首先到达那里:P

下面是删除字符串中重复项的代码。

在此处输入图像描述

Output:

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM