Why StringBuilder class not having Contains method

Tiwari, Nitendra 1 Reputation point
2021-07-29T05:44:21.87+00:00

Dear Team,

In String Class, most "Contains" method is one of the most common methods, similarly, in StringBuilder class, I thought it should also have a similar method, but when I checked there was no such Method.

I knew, there may ways to encounter this problem like using our own extension method, or convert StringBuilder to String etc, but I want to know what is the reason behind not putting "Contains" method in StringBuilder class.

Thanks in Advance

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,940 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Karen Payne MVP 35,421 Reputation points
    2021-07-29T16:59:57+00:00

    Edit: missed the part about why there is no contains. IMHO it's because you can do contains, startswith, endswith etc against .ToString() which keeps StringBuilder focused on it's original intent to build efficient strings.

    If we approach it from .ToString then we are dealing with a string.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using static System.Globalization.DateTimeFormatInfo;
    
    namespace ConsoleNetCoreStringBuilder
    {
        class Program
        {
            public static List<string> MonthNames() => 
                Enumerable.Range(1, 12).Select((index) => CurrentInfo.GetMonthName(index)).ToList();
            static void Main(string[] args)
            {
                StringBuilder builder = new ();
    
                MonthNames().ForEach(month => builder.AppendLine(month));
    
                Console.WriteLine(builder.ToString().Contains("April")); // yes
                Console.WriteLine(builder.ToString().Contains("april")); // no case sensitive
                Console.WriteLine(builder.ToString().Contains("april", StringComparison.OrdinalIgnoreCase)); // yes ignore case
    
                Console.ReadLine();
            }
    
    
    
        }
    
    
    }
    

    And then we can write an extension

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using static System.Globalization.DateTimeFormatInfo;
    
    namespace ConsoleNetCoreStringBuilder
    {
        class Program
        {
            public static List<string> MonthNames() => 
                Enumerable.Range(1, 12).Select((index) => CurrentInfo.GetMonthName(index)).ToList();
            static void Main(string[] args)
            {
                StringBuilder builder = new ();
    
                MonthNames().ForEach(month => builder.AppendLine(month));
    
                Console.WriteLine(builder.Contains("April")); // yes
    
                Console.ReadLine();
            }
    
    
    
        }
    
        public static class Extensions
        {
            public static bool Contains(this StringBuilder sender, string text) => 
                sender.ToString().Contains(text, StringComparison.OrdinalIgnoreCase);
        }
    
    
    }
    
    0 comments No comments

  2. Zhanglong Wu-MSFT 261 Reputation points Microsoft Vendor
    2021-08-10T03:31:47.923+00:00

    Hi @Tiwari, Nitendra

    From the document below, which provide a sample how to achieve contain function.

    public class StringBuilderFinder  
    {  
       private StringBuilder sb;  
       private String text;  
         
       public StringBuilderFinder(StringBuilder sb, String textToFind)  
       {  
          this.sb = sb;  
          this.text = textToFind;  
       }  
         
       public bool SearchAndAppend(String stringToSearch)  
       {  
          sb.Append(stringToSearch);  
          return stringToSearch.Contains(text);  
       }  
    }  
    

    https://learn.microsoft.com/en-us/dotnet/api/system.text.stringbuilder?view=net-5.0#searching-the-text-in-a-stringbuilder-object

    Best regards,
    Zhanglong

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.