Reverse string in Asp c#

In this article I will explain you how to reverse a string programmatically and using inbuilt functions.

Code Behind(Programmatically):

 

 protected void Page_Load(object sender, EventArgs e)
    {
        //===== Asp C# .Net String Reverse through coding.
        string str = "abcdefghijklmnopqrstuvwxyz";

        //====== Variable to store results
        string strResult = string.Empty;


        //===== Get the count of characters to implement loop.Subract 1 from the count as string characters begin with 0 index. 
        //===== "abcd"  has 4 characters but when we loop inside string or array first character has 0 index means 'a' is at 0th position 
        //===== so if we loop from 0 to 4 loop will run 5 times 0-4 and will give error on 4th time. 
        int charCount = str.Count() - 1;

        //==== Loop through each character from end and store it in result variable.
        for (int i = charCount; i >= 0; i--)
        {
            strResult += str[i].ToString();
        }

        Response.Write(strResult);
    }


Using Inbuilt Functions:

 



        //======= Using inbuilt methods
        char[] charArr = str.ToCharArray().Reverse().ToArray();
        str = new string(charArr);
        Response.Write(str);

 
Best quality Asp .Net Ajax Control Toolkit tutorials.

Give your valuable comments.

Name
Email
Comment
3 + 1 =
 

About Us | Terms of Use | Privacy Policy | Disclaimer | Contact Us Copyright © 2012-2024 CodingFusion
50+ C# Programs for beginners to practice