如何从stringbuilder中删除重复元素 [英] How to remove duplicate element from stringbuilder
问题描述
我无法从stringbuilder中删除重复元素
我尝试过:
StringBuilder sb1 = new StringBuilder();
string sep =;
// List< string> term = new List< string>();
foreach(Repeater1.Items中的RepeaterItem项)
{
CheckBoxList CheckBoxList1 =(CheckBoxList)item.FindControl(CheckBoxList1);
foreach(CheckBoxList1.Items中的ListItem item1)
{
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();
}
你的代码对我来说没有意义:
- 你是在内部循环中调用
item.FindControl(Label1)
。因此,当选中多个复选框时,您将多次获得相同的值。
- 您正在设置
Label5
外循环多次。
因此,您将有一个空列表/字符串或多次包含相同文本的字符串/字符串。如果是这样,代码可以简化为:
foreach (RepeaterItem item in Repeater1.Items)
{
CheckBoxList CheckBoxList1 =(CheckBoxList)item.FindControl( < span class =code-string> CheckBoxList1);
foreach (ListItem item1 in CheckBoxList1.Items)
{
if (item1.Selected == true )
{
Label lblname =(标签)item.FindControl( Label1);
Label5.Text = lblname.Text;
break ;
}
}
}如果不是这样,你应该考虑一下你最终想做什么并编写适当的代码。我至少期望
Label5
应该在任何循环之外设置。
ADI @ 345在问题的评论中写道:
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 =(标签)item.FindControl( Label1);
term.Add(lblname.Text);
}
}
}
Label5.Text = 字符串 .Join( ,term.ToArray());
}
这是有效的,但我怎么只得到不同的element.i意味着删除重复的元素..
不确定我理解你好,但是......你可以使用 Enumerable.Distinct(TSource)方法(IEnumerable(TSource))(System.Linq) [ ^ ]。
例如:
List< string> term = new 列表< string>(){ a , a, b, c, c, < span class =code-string> c, d};
var result = string .Join( ,,term.Distinct());
// 生成:a,b,c,d
在你的情况下:
Label5.Text = string .Join( ,term。 Distinct());
应该可以胜任!
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(); }
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:
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.
ADI@345 wrote in the comment to the question:
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:
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:
Label5.Text = string.Join(" ", term.Distinct());
should do the job!
这篇关于如何从stringbuilder中删除重复元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!