Created
March 15, 2024 04:28
-
-
Save baba-s/7e392a811a689772ee811400d3c0fa67 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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