Tuesday 1 October 2013

How to get odd and even Character's position from string and convert odd character in upper case and even character in lower case in ASP.net

 protected void Page_Load(object sender, EventArgs e)
    {
        string name = "Testpr";
        string newname = "";
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 6; i++)
        {
            string textname = name[i].ToString(); // Get Character one by one
            if ((i % 2) == 0) // Check it is odd or even (Means character position)
            {
                newname = (textname).ToUpperInvariant();
            }
            else
            {
                newname = (textname).ToLowerInvariant();
            }
            sb = sb.Append(newname); // Add one by on
        }
        string text = sb.ToString();
        Response.Write(text); // Print in  Page
    }