Click here to Skip to main content
15,912,756 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i am unable to remove duplicate element from stringbuilder

What I have tried:

StringBuilder sb1 = new StringBuilder();
       string sep = "";

       //List<string> term = new List<string>();
       foreach (RepeaterItem item in Repeater1.Items)
       {
           CheckBoxList CheckBoxList1 = (CheckBoxList)item.FindControl("CheckBoxList1");
           foreach (ListItem item1 in CheckBoxList1.Items)
           {
               if (item1.Selected == true)
               {
                 Label lblname = (Label)item.FindControl("Label1");
                 sb1.AppendFormat("{0}{1}", sep, lblname.Text);

               }

           }
           List<string> itemList = sb1.ToString().ToUpper().Split(',').ToList<string>();
           Label5.Text = itemList.ToString();
       }
Posted
Updated 22-May-18 23:55pm
Comments
Jochen Arndt 23-May-18 4:57am    
Perform the check before appending.

However, why do you use a StringBuilder here?
Why not use a List<string> and add items not existing already in the list?
F-ES Sitecore 23-May-18 4:59am    
What do you mean by "duplicate element"? Stringbuilder is just text. If you mean that you have checkbox items with the same label and you don't want duplicate labels then the solution to add is to not add the duplicates to the string builder in the first place. You could add the text to a List<string> instead and do a .Distinct() on that list to remove the duplicates then add those to the string builder.
ADI@345 23-May-18 5:41am    
List<string> term = new List<string>();

foreach (RepeaterItem item in Repeater1.Items)
{
CheckBoxList CheckBoxList1 = (CheckBoxList)item.FindControl("CheckBoxList1");
foreach (ListItem item1 in CheckBoxList1.Items)
{
if (item1.Selected == true)
{
Label lblname = (Label)item.FindControl("Label1");
term.Add(lblname.Text);

}

}


}

Label5.Text = string.Join(" ", term.ToArray());
}


this is working , but how i get only distinct element.i mean remove duplicate element..
Maciej Los 23-May-18 5:45am    
Please, define "duplicate element"...

Your code makes not really sense for me:
  • You are calling item.FindControl("Label1") in the inner loop. So you will get the same value multiple times when multiple check boxes are selected.
  • You are setting the Label5 within the outer loop multiple times.

As a result, you would have an empty list / string or one containing the same text multiple times. If that is intended, the code can be simplified to:
c+
foreach (RepeaterItem item in Repeater1.Items)
{
    CheckBoxList CheckBoxList1 = (CheckBoxList)item.FindControl("CheckBoxList1");
    foreach (ListItem item1 in CheckBoxList1.Items)
    {
        if (item1.Selected == true)
        {
            Label lblname = (Label)item.FindControl("Label1");
            Label5.Text = lblname.Text;
            break;
        }
    }
}
If that is not intended, you should think about what you finally want to do and write appropriate code. I would at least expect that Label5 should be set outside any loop.
 
Share this answer
 
ADI@345 wrote in the comment to the question:

C#
List<string> term = new List<string>();

foreach (RepeaterItem item in Repeater1.Items)
{
    CheckBoxList CheckBoxList1 = (CheckBoxList)item.FindControl("CheckBoxList1");
    foreach (ListItem item1 in CheckBoxList1.Items)
    {
        if (item1.Selected == true)
        {
            Label lblname = (Label)item.FindControl("Label1");
            term.Add(lblname.Text);
        }
    }
}

Label5.Text = string.Join(" ", term.ToArray());
}


this is working , but how i get only distinct element.i mean remove duplicate element..


Not sure i understand you well, but... you can use Enumerable.Distinct(TSource) Method (IEnumerable(TSource)) (System.Linq)[^].

For example:
C#
List<string> term = new List<string>(){"a", "a", "b", "c", "c", "c", "d"};
var result = string.Join(",", term.Distinct());
//produces: a,b,c,d


In your case:
C#
Label5.Text = string.Join(" ", term.Distinct());

should do the job!
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900