You must Sign In to post a response.
  • Category: .NET

    How to use String.Split( ) ?

    The forum thread has not been reviewed by Editors yet. Readers are advised to use their best judgement before accessing this thread.
    This thread will be reviewed shortly.
    If you think this thread contain inappropriate content, please report to webmaster.
    Dear All

    My case is....I need to split this string 01/02/06 (e.g.) and then re-arrange it to 02/01/06 in ASP.NET (C#). I tried the String.Split() method but the output is not the expected one:

    THE CODE:

    private void submitButton_Click(object sender, System.EventArgs e)
    {
    string delimStr = "/";

    char [] delimiter = delimStr.ToCharArray();
    string [] split = null;

    string words = queryJobStartDate.Text.ToString();

    for (int x = 1; x <= 3; x++)
    {
    split = words.Split(delimiter, x);

    foreach (string s in split)
    {
    if (x == 1) {Label101.Text = s;}
    if (x == 2) {Label102.Text = s;}
    if (x == 3) {Label103.Text = s;}
    }
    }
    }

    THE OUTPUT
    01/02/2006
    02/2006
    2006

    I expected the output should be
    01
    02
    2006

    So, could any one help me? May I have some other example or advice to follow ?

    Thanks
  • #59318
    The easier way is to use queryJobStartDate.Text.SubString();
    it will cut strings. Make many cuts and concatenate. Thanks

  • #59328
    This is VB.Net Code you can change this code to C#.

    Dim myString As String = "01/02/06 "

    Dim delimStr As String = "/"

    Dim delimiter As Char() = delimStr.ToCharArray()

    Dim s As String

    For Each s In myString.Split(delimiter)
    Response.Write((s.ToString() + "<br>"))
    Next

  • #59344
    Hi,

    I have modified your code as given below


    private void submitButton_Click(object sender, System.EventArgs e)
    {


    string delimStr = "/";
    char[] delimiter = delimStr.ToCharArray();
    string[] split = null;
    string words = queryJobStartDate.Text.ToString();
    split = words.Split(delimiter, 3);
    Label101.Text = split[0];
    Label102.Text = split[1];
    Label103.Text = split[2];
    }


  • This thread is locked for new responses. Please post your comments and questions as a separate thread.
    If required, refer to the URL of this page in your new post.