How do I reverse a string?

Category: System, viewed: 342 time(s).

In the following example you'll see how to reverse a string. We use the Array.Reverse() method to reverse the string. First we need to convert the string to char[] and then calling the Array.Reverse method and passes the character array. After this method reverse the array order we create a new string from this character array.

using System;

namespace Kodecsharp.Example.System
{
    class ReverseString
    {
        public static void Main(string[] args)
        {
            string text = "The quick brown fox jumps over the lazy dog";
            char[] c = text.ToCharArray();

            Array.Reverse(c);

            string reversedText = new string(c);
            Console.WriteLine("Reversed Text = " + reversedText);
            Console.ReadLine();
        }
    }
}
Download Hundreds of Complimentary Industry Resources

Get hundreds of popular Industry magazines, white papers, webinars, podcasts, and more; all available at no cost to you. With more than 600 complimentary offers, you'll find plenty of titles to suit your professional interests and needs. Click Here and Sign up today!

Our Friends

Statistics

Locations of visitors to this page