Skip to content

Instantly share code, notes, and snippets.

@baba-s
Created March 15, 2024 04:28
Show Gist options
  • Save baba-s/7e392a811a689772ee811400d3c0fa67 to your computer and use it in GitHub Desktop.
Save baba-s/7e392a811a689772ee811400d3c0fa67 to your computer and use it in GitHub Desktop.
using System.Collections.Generic;
using System.Text;
namespace Kogane
{
/// <summary>
/// 配列の拡張メソッド
/// </summary>
public static class ArrayExtensionMethods
{
//================================================================================
// 関数(static)
//================================================================================
/// <summary>
/// 二次元配列のように扱っている一次元配列を文字列に変換して返します
/// </summary>
public static string JoinToString<T>
(
this IReadOnlyList<T> self,
int column,
int row
)
{
return self.JoinToString
(
column: column,
row: row,
separator: string.Empty
);
}
/// <summary>
/// 二次元配列のように扱っている一次元配列を文字列に変換して返します
/// </summary>
public static string JoinToString<T>
(
this IReadOnlyList<T> self,
int column,
int row,
string separator
)
{
var stringBuilder = new StringBuilder();
for ( var y = 0; y < row; y++ )
{
for ( var x = 0; x < column; x++ )
{
stringBuilder.Append( self[ x + y * column ] );
if ( x < column - 1 )
{
stringBuilder.Append( separator );
}
}
if ( y < row - 1 )
{
stringBuilder.AppendLine();
}
}
return stringBuilder.ToString();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment