Muroiwa263
Muroiwa263

Reputation: 33

VB.NET remove specific chars between two characters in a string

In vb.net how do i remove a character from string which occurs between two known characters in a series.For example how do you remove commas from the number occurring between the hashtag

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

Upvotes: 1

Views: 1423

Answers (2)

Tim Schmelter
Tim Schmelter

Reputation: 460108

You can use this simple and efficient approach using a loop and a 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()

Upvotes: 3

A Friend
A Friend

Reputation: 2750

Because I'm bad at regex:

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)

Upvotes: -1

Related Questions