如何在所有数组项中添加一些字符串?

时间:2017-04-19 11:30:12

标签: arrays vb.net

我有一个如下所示的数组

(0) = "apple"
(1) = "orange"

如何在数组中的所有项目中添加一些字符串?就像苹果变成“苹果”一样,橙色变成了橙色'

被修改

 Private Sub test()
        Dim txtReader As TextReader = New StreamReader("data.csv")
        Dim parser = New CsvParser(txtReader)
        Dim str As String = ""

        'Ignore first line
        parser.Read()

        While True
            Dim row = parser.Read()
            If row Is Nothing Then
                Exit While
            End If

            str &= $"({String.Join(",", row)}),"
        End While

        str_record = str.TrimEnd(",")
 End Sub

Private Sub Model_Insert()
    Dim data As String = ""
    Dim query As String = "INSERT INTO main_item(item_code,item_name,item_desc,item_unitprice,item_barcode,dept_id,cat_id,gst_id,set_item,active)" &
                          "VALUES " & str_record & ""

    Using cmd = New MySqlCommand(query, conn)
        cmd.ExecuteNonQuery()
    End Using
End Sub

我试图创建一个字符串并在INSERT INTO中使用它

2 个答案:

答案 0 :(得分:4)

使用For - 循环:

Dim array = {"apple", "orange"}
For i As Int32 = 0 To array.Length - 1
    array(i) = $"'{array(i)}'"
Next

如果您还不能使用字符串插值,请使用String.Format(或字符串连接):

For i As Int32 = 0 To array.Length - 1
    array(i) = String.Format("'{0}'", array(i))
Next

答案 1 :(得分:0)

如果您不介意重新创建阵列,可以使用Linq Select

Dim testarray As String() = New String() {"orange", "apple"}
testarray = testarray.Select(Function(x) String.Format("'{0}'", x)).ToArray()