How to reverse a string using C#?

王林
Release: 2023-08-22 23:09:08
forward
1660 people have browsed it

How to reverse a string using C#?

To reverse a string, use the Array.Reverse() method.

Set the string to be reversed −

string str = "Amit";
Copy after login

In the above method, we convert the string into a character array −

char[] ch = str.ToCharArray();
Copy after login

and then use the Reverse() method. The Chinese translation of

Array.Reverse(ch);
Copy after login

Example

is:

Example

using System;
namespace Demo {
   class Program {
      static void Main(string[] args) {

         string str = "Amit";
         char[] ch = str.ToCharArray();

         Array.Reverse(ch);

         foreach(var items in ch) {
            Console.WriteLine(items);
         }

         Console.ReadLine();
      }
   }
}
Copy after login

The above is the detailed content of How to reverse a string using C#?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template