Split the sentence after colon in asp.net

Leave a Comment
In this article, we have learn how to split the sentence into worlds after colon(,). For this purpose we have used split method and also use trim method to remove the extra space from the words.

Open visual studio 2010 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 to 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
Demo of Split and Trim Method

0 comments:

Post a Comment