我希望有额外的字符作为分隔符我的字符串

时间:2016-07-28 02:33:45

标签: vb.net

Sub SaveInLogs(isThereNewLogs As Boolean, value As String)

    Dim list As List(Of String)
    Try
        list = Strings.Split(value, "#-!-#").ToList
        list(2) = "Incoming"
        For Each word In list
            MsgBox(list)
        Next
    Catch ex As Exception
    End Try

End Sub

参数“值”的值是这样的: 7/28/2016 10:19:27 AM# - ! - #Alex17282016# - ! - #Outgoing# - ! - #Alex# - ! - #Alex# - ! - #Text# - ! - #1ST

然后我将值拆分并将单词Outgoing更改为Incoming。

现在我希望我的字符串再次看起来像这样 7/28/2016 10:19:27 AM# - ! - #Alex17282016# - ! - #Incoming# - ! - #Alex# - ! - #Alex# - ! - #Text# - ! - #1ST

对不起我的语法。我希望你理解我的问题

1 个答案:

答案 0 :(得分:0)

下面我已经编辑了你的代码,你必须在将“Outgoing”改为“Incoming”之后按照你想要的方式制作字符串:

Sub SaveInLogs(isThereNewLogs As Boolean, value As String)

Dim list As List(Of String)
Dim newValue as String = ""
Try
    list = Strings.Split(value, "#-!-#").ToList
    list(2) = "Incoming"

    For Each word In list
        newValue = newValue & word
    Next

    value = newValue
Catch ex As Exception
End Try

End Sub

或者在评论中指出Mark可以使用string.join而不是循环:

Sub SaveInLogs(isThereNewLogs As Boolean, value As String)

Dim list As List(Of String)
Try
    list = Strings.Split(value, "#-!-#").ToList
    list(2) = "Incoming"

    Dim newValue As String = String.Join("#-!-#", list)

Catch ex As Exception
End Try

End Sub

下面是我认为可以更好地运行的代码,因为它只需要一行代码,而不需要将字符串分开并将其重新组合在一起:

Sub SaveInLogs(isThereNewLogs As Boolean, value As String)

    value = value.replace("#-!-#Outgoing#-!-#", "#-!-#Incoming#-!-#")

End Sub