[C#, VB.NET] 配列文字列を区切り文字を使って連結

String.Join メソッドは、配列の要素を指定した区切り文字で連結する。

String.Join メソッド - msdn
http://msdn.microsoft.com/ja-jp/library/system.string.join%28v=vs.110%29.aspx

var dat = new string[] { "Michael", "Christopher", "Joshua", "Matthew", "Daniel" };
var ret = String.Join(",", dat);
// Michael,Christopher,Joshua,Matthew,Daniel
Console.WriteLine(ret);

var dat2 = new List() { "Robert", "James", "Nicholas" };
var ret2 = String.Join(",", dat2);
// Robert,James,Nicholas
Console.WriteLine(ret2);
VB.NET の場合も同等の動きをするのだが、Join 関数は VB にしかない。
C# と比較するとなかなか興味深い動作である。
Dim data As String() = {"Michael", "Christopher", "Joshua", "Matthew", "Daniel"}
Dim result = String.Join(",", data)
' Michael,Christopher,Joshua,Matthew,Daniel
Console.WriteLine(result)

' Join 関数は区切り文字を省略できる。
Dim result2 = Join(data)
' Michael Christopher Joshua Matthew Daniel
Console.WriteLine(result2)

' Join 関数で区切り文字を指定する場合は、第二引数に指定する。
Dim result3 = Join(data, ",")
' Michael,Christopher,Joshua,Matthew,Daniel
Console.WriteLine(result3)
Join 関数 (Visual Basic) - msdn
http://msdn.microsoft.com/ja-jp/library/b65z3h4h%28v=vs.90%29.aspx

このブログの人気の投稿

コピーした行の挿入が表示されない時はフィルタされていないかチェック

Excel で一部の図形だけ固定する