Split Method in C# Example

Updated on     Kisan Patel

This post will explain you how to split the sentence into worlds after colon(,). To split the sentence, we have used split method and also use trim method to remove the extra space from the words.

Open visual studio and create a new website called SplitSentenceDemo.

Now, add Default.aspx web form into Website and add below code :

<body>
   <form id="form1" runat="server">
     <div>
       <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
       <asp:Button ID="Button1" runat="server" Text="Split" onclick="Button1_Click" />
       <br />
       <br />
       <br />
       <asp:Label ID="Label1" runat="server"></asp:Label>
     </div>
  </form>
</body>

Now, Create the Click event for Id of Button1 and add the below code :

protected void Button1_Click(object sender, EventArgs e)
{
   string strTag = TextBox1.Text;
   string[] words = strTag.Split(',');
   foreach (string s in words)
   {
        string sl = s.ToLower();
        string st = sl.Trim();
        Label1.Text += st + "</br>";
   }
}

In above code we have used two methods split and trim.

Split method can be used to split the sentence and trim method can be used to remove extra space from the word.

Demo

split-method-demo


ASP.NET C#

Leave a Reply