c# - How to find a substring in a StringBuilder

c# example - find a substring in stringbuilder
The following asp.net c# example code demonstrate us how can we find a substring within a StringBuilder object programmatically at run time in an asp.net application. We can explain the tutorial as 'how can we determine a StringBuilder object contains a specified substring'.

.Net framework's StringBuilder Class represent a mutable string of characters. StringBuilder Class does not have any method or property to find a substring from it. So, we need to take help from .net framework's String Class. We can convert a StringBuilder object's value to a System.String object by using StringBuilder.ToString() method.

String Class String.Contains() method return a value indicating whether a specified substring occurs within this string. String.Contains() method require to pass a parameter named 'value' which value data type is Sysetm.String and represent the substring to seek/find/search from instance.

String.Contains() method return a Boolean value. It return 'true' if the specified substring occurs (found) within this string or parameter pass an empty string; otherwise it return 'false'.

This is the very simple way to find a substring within the StringBuilder object's sequence of characters.
find-a-substring-in-stringbuilder

<%@ Page Language="C#" AutoEventWireup="true"%>
<%@ Import Namespace="System.IO" %>
  
<!DOCTYPE html>
  
<script runat="server"> 
    protected void Button1_Click(object sender, System.EventArgs e)  
    {
        StringBuilder stringb = new StringBuilder();
        stringb.Append("Snowy Owl.");
        stringb.AppendLine();

        stringb.Append("Tawny Owl.");
        stringb.AppendLine();

        stringb.Append("Jamaican Owl.");
        stringb.AppendLine();

        //this line find substring in stringbuilder.
        Boolean result = stringb.ToString().Contains("Cave Swiftlet");
        
        //this is a technique to find substring in stringbuilder.
        Boolean result2 = stringb.ToString().Contains("Tawny Owl");

        Label1.Text = stringb.ToString();
        Label1.Text += "<br /><br />stringbuilder contains('Cave Swiftlet'): " + result.ToString();
        Label1.Text += "<br />stringbuilder contains('Tawny Owl'): " + result2.ToString();
    }  
</script>  
  
<html xmlns="http://www.w3.org/1999/xhtml">  
<head id="Head1" runat="server">  
    <title>c# example - find a substring in stringbuilder</title>  
</head>  
<body>  
    <form id="form1" runat="server">  
    <div>  
        <h2 style="color:MidnightBlue; font-style:italic;">  
            c# example - find a substring in stringbuilder
        </h2>  
        <hr width="550" align="left" color="Gainsboro" />  
        <br />  
        <asp:Label   
            ID="Label1"   
            runat="server"  
            Font-Size="Large"  
            >  
        </asp:Label>  
        <br /><br /><br />
        <asp:Button   
            ID="Button1"   
            runat="server"   
            Text="find a substring in stringbuilder"  
            OnClick="Button1_Click"
            Height="40"  
            Font-Bold="true"  
            />  
    </div>  
    </form>  
</body>  
</html>
More c# examples