title: In C#, best way to check if stringbuilder contains a substring
body: <p>I have an existing <code>StringBuilder</code> object, the code appends some values and a delimiter to it.</p>
  
  <p>I want to modify the code to add the logic that before appending the text, it will check if it already exists in the <code>StringBuilder</code>. If it does not, only then will it append the text, otherwise it is ignored.</p>
  
  <p>What is the best way to do so? Do I need to change the object to <code>string</code> type? I need the best approach that will not hamper performance.</p>
  
  <pre><code>public static string BuildUniqueIDList(context RequestContext)
  {
      string rtnvalue = string.Empty;
      try
      {
          StringBuilder strUIDList = new StringBuilder(100);
          for (int iCntr = 0; iCntr &lt; RequestContext.accounts.Length; iCntr++)
          {
              if (iCntr &gt; 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;
  }
  </code></pre>
  
  <p>I am not sure if having something like this will be efficient:</p>
  
  <pre><code>if (!strUIDList.ToString().Contains(RequestContext.accounts[iCntr].uniqueid.ToString()))
  </code></pre>