【发布时间】:2011-07-04 09:57:45
【问题描述】:
我有一个现有的StringBuilder 对象,代码向它附加了一些值和一个分隔符。
我想修改代码以添加在附加文本之前它会检查它是否已经存在于StringBuilder 中的逻辑。如果没有,那么它只会附加文本,否则将被忽略。
最好的方法是什么?我需要将对象更改为string 类型吗?我需要不会影响性能的最佳方法。
public static string BuildUniqueIDList(context RequestContext)
{
string rtnvalue = string.Empty;
try
{
StringBuilder strUIDList = new StringBuilder(100);
for (int iCntr = 0; iCntr < RequestContext.accounts.Length; iCntr++)
{
if (iCntr > 0)
{
strUIDList.Append(",");
}
// need to do somthing like:
// strUIDList.Contains(RequestContext.accounts[iCntr].uniqueid) then continue
// otherwise append
strUIDList.Append(RequestContext.accounts[iCntr].uniqueid);
}
rtnvalue = strUIDList.ToString();
}
catch (Exception e)
{
throw;
}
return rtnvalue;
}
我不确定这样的东西是否有效:
if (!strUIDList.ToString().Contains(RequestContext.accounts[iCntr].uniqueid.ToString()))
【问题讨论】:
标签: c# string contains stringbuilder