VB.NET删除字符串

时间:2017-05-24 09:48:18

标签: string vb.net

在vb.net中如何从字符串中删除字符串中的两个已知字符之间的字符。例如,如何从标签之间的数字中删除逗号

余额,#163,464.24#,Cashbook结算余额:,#86,689.45#,Money,End

2 个答案:

答案 0 :(得分:3)

您可以使用循环和StringBuilder

来使用这种简单有效的方法
Dim text = "Balance,#163,464.24#,Cashbook Closing Balance:,#86,689.45#,Money,End"
Dim textBuilder As New StringBuilder()
Dim inHashTag As Boolean = False

For Each c As Char In text
    If c = "#"c Then inHashTag = Not inHashTag ' reverse Boolean
    If Not inHashTag OrElse c <> ","c Then
        textBuilder.Append(c) ' append if we aren't in hashtags or the char is not a comma
    End If
Next
text = textBuilder.ToString()

答案 1 :(得分:-1)

因为我对正则表达式不好:

Dim str = "Balance,#163,464.24#,Cashbook Closing Balance:,#86,689.45#,Money,End"
Dim split = str.Split("#"c)
If UBound(split) > 1 Then
    For i = 1 To UBound(split) Step 2
        split(i) = split(i).Replace(",", "")
    Next
End If
str = String.Join("#", split)