LoginSignup
1
0

More than 5 years have passed since last update.

[VB.NET] くくってから Join()

Posted at

はじめに

Join() 関数は文字列配列を結合してくれますが、引用符でくくってから結合したいときがあります。

たとえば CSV 文字列を作る際にダブルクォーテーションでくくってからカンマで結合したい場合などです。

Join() 関数に似た使い方ができるような新しい関数を作ってみます。

スニペット

Public Shared Function JoinWithQuotation(ByVal SourceArray As String(),
                                         Optional ByVal Delimiter As String = " ",
                                         Optional ByVal Quotation As String = """") As String
    Dim quotedStrings As String() = SourceArray.Select(Function(s As String) Quotation & s & Quotation).ToArray()
    Return Join(quotedStrings, Delimiter)
End Function

または

Public Shared Function JoinWithQuotation(ByVal SourceArray As String(),
                                         Optional ByVal Delimiter As String = " ",
                                         Optional ByVal Quotation As String = """") As String
    Dim quotedStrings As String() = Array.ConvertAll(SourceArray, Function(s As String) Quotation & s & Quotation)
    Return Join(quotedStrings, Delimiter)
End Function

さいごに

※ .NET 3.5 以降なら動くと思います。

参考:c# - Join list of string to comma separated and enclosed in single quotes - Stack Overflow

キーワード:Join array with single or double quote quotes quotation

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0