for循环用于字符串变量

ire*_*egy 2 vb.net string for-loop

这是我的代码 -

for i as integer = 0 to rows.count - 1
   output &= "Name =" & row(i)("Name")
   output &= "lastName =" & row(i)("lastName")
... 50 more fields
next
Run Code Online (Sandbox Code Playgroud)

我需要输出像这样

Applicant1Name = MikeApplicant1lastName = ditkaApplicant2Name = TomApplicant2lastName = Brady ...

如何在不添加以下代码50次的情况下执行此操作 - 输出&="申请人"&i.tostring()+ 1&"名称="和行(i)("名称")...等等.有没有办法制作一个for循环并一次性运行申请人1,2,3,4 .... 谢谢

Joe*_*ton 6

尝试:

Dim output as New StringBuilder("")

For i as Integer = 0 To rows.Count - 1
    output.append("Applicant" + i.ToString())
    Foreach(col as DataColumn in dt.Columns)  ' The datatable where your rows are
        Dim colName as string = col.ColumnName
        output.append(colName & "=" & rows(i)(colName).ToString())
    Next
    If i < rows.Count - 1 Then output.Append("|")
Next
Run Code Online (Sandbox Code Playgroud)

StringBuilder对于字符串连接更快,如果你把你的行放在一个数据表中(我假设它正在发生,因为它就是你访问它们的样子),那么你可以遍历顶层的列名.