コガネブログ

平日更新を目標に Unity や C#、Visual Studio、ReSharper などのゲーム開発アレコレを書いていきます

【C#】指定された文字列に含まれるタブをスペースに変換する関数

ソースコード

using System.Text;

public static class TabToSpaceConvertor
{
    private static int GetNearestTabStop( int currentPosition, int tabLength )
    {
        if ( currentPosition % tabLength == 1 )
        {
            currentPosition += tabLength;
        }
        else
        {
            for ( int i = 0; i < tabLength; i++, currentPosition++ )
            {
                if ( ( currentPosition % tabLength ) == 1 )
                {
                    break;
                }
            }
        }

        return currentPosition;
    }

    public static string Process( string input, int tabLength )
    {
        if ( string.IsNullOrEmpty( input ) ) return input;

        var output = new StringBuilder();

        int positionInOutput = 1;

        foreach ( var c in input )
        {
            switch ( c )
            {
                case '\t':
                    int spacesToAdd = GetNearestTabStop( positionInOutput, tabLength ) - positionInOutput;
                    output.Append( new string( ' ', spacesToAdd ) );
                    positionInOutput += spacesToAdd;
                    break;

                case '\n':
                    output.Append( c );
                    positionInOutput = 1;
                    break;

                default:
                    output.Append( c );
                    positionInOutput++;
                    break;
            }
        }
        return output.ToString();
    }
}

使用例

var text = "ピカチュウ\tカイリュー\t\tヤドラン\n\tピジョン";
text = TabToSpaceConvertor.Process( text, 4 );

参考サイト様