Sunday, July 26, 2015

How to split the string in Asp.net

In this article today I am going to explain How to split the string in Asp.net

Description:

Splits a string into substrings that are based on the characters in an array. Like enter text with space, separated by ‘/’ or regular expressions etc.

Implementation:
Example 1
 In this example I am going to split the text entered with space.

HTML Markup:
<table>
        <tr><td>Enter Test</td><td></td><td>
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></td></tr>
         <tr><td></td><td></td><td></td></tr>
        <tr><td></td><td></td><td>
            <asp:Button ID="Button1" runat="server" Text="Split Text"/></td></tr>
    </table>

Write the code on button click to split the entered text

C#:
  protected void Button1_Click(object sender, EventArgs e)
    {
        string input = TextBox1.Text.Trim();
        string[] StrSplit = input.Split(' ');
        foreach (var splitstr in StrSplit)
        {
            Response.Write(splitstr);
            Response.Write("<br>");
        }
    }

VB:
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim input As String = TextBox1.Text.Trim()
        Dim StrSplit As String() = input.Split(" ")
        For Each splitstr In StrSplit
            Response.Write(splitstr)
            Response.Write("<br>")
        Next
    End Sub
Result:
How to split the string in Asp.net

Example 2
Split the text after slash (/) e.g. split the date of birth

Add the namespace to code file
C#:
using System.Text.RegularExpressions;
VB:
Imports System.Text.RegularExpressions

Write the code on button click.
C#:
  protected void Button1_Click(object sender, EventArgs e)
    {
        string input = TextBox1.Text.Trim();
        String pattern = @"/";
        string[] StrSplit = Regex.Split(input, pattern);
        foreach (var splitstr in StrSplit)
        {
            Response.Write(splitstr);
            Response.Write("<br>");
        }
    }

VB:
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim input As String = TextBox1.Text.Trim()
        Dim pattern As [String] = "/"
        Dim StrSplit As String() = Regex.Split(input, pattern)
        For Each splitstr In StrSplit
            Response.Write(splitstr)
            Response.Write("<br>")
        Next
    End Sub
If you like split the text after hyphen (-) replace the slash (/) sign in pattern.
Result:
How to split the string in Asp.net

Example 3
Spit the entered text in single letters. E.g. if I enter the Dollar in textbox, It splits into single letter D o l l a r.
C#:
protected void Button1_Click(object sender, EventArgs e)
    {
        string input = TextBox1.Text.Trim();
        char[] character = input.ToCharArray();
        foreach (char c in character)
        {
            Response.Write(c);
            Response.Write("<br>");
        }
    }

VB:
   Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim input As String = TextBox1.Text.Trim()
        Dim character As Char() = input.ToCharArray()
        For Each c As Char In character
            Response.Write(c)
            Response.Write("<br>")
        Next
    End Sub

Result:
How to split the string in Asp.net
    In this article we have learn how to split the string in Asp.net (C#, VB). I hope you enjoyed this article. 

No comments:

Post a Comment