【问题标题】:VB.NET remove specific chars between two characters in a stringVB.NET 删除字符串中两个字符之间的特定字符
【发布时间】:2017-10-24 14:05:34
【问题描述】:

在 vb.net 中,我如何从出现在系列中两个已知字符之间的字符串中删除一个字符。例如,如何从主题标签之间出现的数字中删除逗号

Balance,#163,464.24#,Cashbook Closing Balance:,#86,689.45#,Money,End

【问题讨论】:

  • 在枚举字符时,使用hashtag 的标志并在标志为true 时删除逗号。在下次出现# 时将标志设置为false

标签: string vb.net


【解决方案1】:

您可以通过循环和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()

【讨论】:

  • 这绝对是比我的更安全的建议。
  • @tim schmelter 谢谢你完美的工作,也谢谢你@A Friend
【解决方案2】:

因为我不擅长正则表达式:

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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-07-01
    • 2014-09-01
    • 2017-04-12
    • 2012-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多