将数组或列表输出到字符串

时间:2013-06-11 13:27:39

标签: arrays string list vba

我编写了以下代码片段(在Outlook中的VBA中)以尝试将我的列表输出到字符串。这种方式可能并不完美;它编译,但我还没有测试过;无论如何,我想知道你是否有任何关于如何在VBA或VB.NET中输出数组或列表到字符串的替代想法。这种方式让我觉得有点陪审团操纵,而且我无法找到任何“标准”或“官方”方式来完成这项工作。

由于我认为这段代码可行,所以我没有其他选择,但我对此非常陌生,我想尽可能多地从这个项目中学习。非常感谢您提供的任何帮助!

    Dim nplct As Integer
    Dim npl As Integer
    Dim strNotpresentList As String
    strNotpresentList = ""
    npl = 0
    nplct = notpresentList.Count
    For Each Computer In notpresentList
        If npl <> nplct Then

            If strNotpresentList = "" Then
                    strNotpresentList = notpresentList(npl)
                    npl = npl + 1
                Else
                    strNotpresentList = strNotpresentList & ", " & notpresentList(np1)
                    npl = npl + 1
                End If


            Else
                strNotpresentList = strNotpresentList & ", " & notpresentList(np1) & "."
            End If

    Next Computer

2 个答案:

答案 0 :(得分:2)

在VB.NET中:

strNotpresentList = String.Join(", ", notpresentList)

答案 1 :(得分:2)

以下是VBA:

Dim strNotPresentList as String
strNotPresentList = Join(notpresentList, ",") & "."

或者如果你想要它在同一行:

Dim strNotPresentList As String: strNotPresentList = Join(notpresentList, ",") & "."

对于VB.Net

Dim strNotPresentList As String = String.Join(", ", notpresentList) + "."