In C#, best way to check if stringbuilder contains a substring

26,335

Personally I would use:

return string.Join(",", RequestContext.accounts
                                      .Select(x => x.uniqueid)
                                      .Distinct());

No need to loop explicitly, manually use a StringBuilder etc... just express it all declaratively :)

(You'd need to call ToArray() at the end if you're not using .NET 4, which would obviously reduce the efficiency somewhat... but I doubt it'll become a bottleneck for your app.)

EDIT: Okay, for a non-LINQ solution... if the size is reasonably small I'd just for for:

// First create a list of unique elements
List<string> ids = new List<string>();
foreach (var account in RequestContext.accounts)
{
    string id = account.uniqueid;
    if (ids.Contains(id))
    {
        ids.Add(id);
    }
}

// Then convert it into a string.
// You could use string.Join(",", ids.ToArray()) here instead.
StringBuilder builder = new StringBuilder();
foreach (string id in ids)
{
    builder.Append(id);
    builder.Append(",");
}
if (builder.Length > 0)
{
    builder.Length--; // Chop off the trailing comma
}
return builder.ToString();

If you could have a large collection of strings, you might use Dictionary<string, string> as a sort of fake HashSet<string>.

Share:
26,335
Sri Reddy
Author by

Sri Reddy

Updated on July 09, 2022

Comments

  • Sri Reddy
    Sri Reddy almost 2 years

    I have an existing StringBuilder object, the code appends some values and a delimiter to it.

    I want to modify the code to add the logic that before appending the text, it will check if it already exists in the StringBuilder. If it does not, only then will it append the text, otherwise it is ignored.

    What is the best way to do so? Do I need to change the object to string type? I need the best approach that will not hamper performance.

    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;
    }
    

    I am not sure if having something like this will be efficient:

    if (!strUIDList.ToString().Contains(RequestContext.accounts[iCntr].uniqueid.ToString()))
    
  • Sri Reddy
    Sri Reddy about 13 years
    My bad, I should have mentioned it, can I do this without LINQ? In .net 2.0?
  • Jon Skeet
    Jon Skeet about 13 years
    @user465876: You can, but personally I'd get hold of LINQBridge instead... LINQ is so useful, it's worth getting hold of the backport.
  • Sri Reddy
    Sri Reddy about 13 years
    Jon, thanks for the tip. Soon we will be moving to 3.5 and then I will defiently use LINQ to the max. But for the timebeing, I need to stick to non-LINQ solution :( If you don't mind, can you tell me how to do this in 2.0 without LINQ/LINQBridge.
  • Jon Skeet
    Jon Skeet about 13 years
    @user465876: Okay, I've added an alternative for .NET 2.
  • Sri Reddy
    Sri Reddy about 13 years
    thanks Jon. It is really helpful. I will keep in mind about LINQ.