#author("2020-02-11T16:02:22+09:00","","")
#navi(../)
* Stringの配列をCSVにする方法 [#ud0acea0]
配列に格納した文字列をCSVにしたい場合は、String.Joinメソッドが便利です。~
ループですると最後のカンマ不要処理など面倒ですよね。~

以下にStringの配列をCSVに変換するサンプルコードを紹介します。

#htmlinsert(windev-top.html)
#contents

* 関連サイト [#j4dfbef4]
-[[Microsoft | .NET String.Join メソッド>https://docs.microsoft.com/ja-jp/dotnet/api/system.string.join]]

* 動作確認環境 [#p33d2e70]
-Windows 10
-Visual Studio 2019
-コンソールアプリケーション
-.NET Core 3.1

* サンプルコード [#tae783b9]
以下に C#, Visual Basic(VB)のサンプルコードを紹介します。

** C# [#f4a9c704]
 using System;
 
 class Program
 {
     static void Main(string[] args)
     {
         string[] ary = { "Windows", "macOS", "Linux", "BSD" };
         Console.WriteLine(string.Join(",", ary));
     }
 }

** Visual Basic(VB) [#j2e37962]
 Module Program
     Sub Main(args As String())
         Dim ary As String() = {"Windows", "macOS", "Linux", "BSD"}
         Console.WriteLine(String.Join(",", ary))
     End Sub
 End Module

* 実行結果 [#s90f2868]
上記のサンプルコードを実行すると以下の出力になります。
#ref(01.png)

以上、Stringの配列をCSV文字列に変換する方法でした。


トップ   編集 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS