如何使用 C# 反转字符串?

csharpprogrammingserver side programming更新于 2025/7/12 19:07:17

要反转字符串,请使用 Array.Reverse() 方法。

设置要反转的字符串 −

string str = "Amit";

在上面的方法中,我们将字符串转换为字符数组 −

char[] ch = str.ToCharArray();

然后使用 Reverse() 方法。

Array.Reverse(ch);

示例

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();
      }
   }
}

相关文章