C# 文字列の文字を反転させる

  • 作成日 2021.01.08
  • 更新日 2022.06.30
  • C#
C# 文字列の文字を反転させる

C#で、ConcatとReverseを使用して、文字列の文字を反転させるサンプルコードを記述してます。

環境

  • OS windows10 pro 64bit
  • Microsoft Visual Studio Community 2019 Version 16.7.1

ConcatとReverse使い方

ConcatとReverseを使用すると、文字列の文字を反転させることが可能です。

using System;
using System.Collections.Generic;
using System.Linq;

namespace testapp
{
    class Program
    {
        static void Main(string[] args)
        {
            string str = "mebee";

            Console.WriteLine(string.Concat(str.Reverse()));
            // eebem
        }
    }
}

配列にしてから行う方法もあります。

using System;
using System.Collections.Generic;

namespace testapp
{
    class Program
    {
        static void Main(string[] args)
        {
            string str = "mebee";

            // 配列にしてから行う
            char[] ch = str.ToCharArray();
            Array.Reverse(ch);

            Console.WriteLine(new string(ch));
            // eebem
        }
    }
}