Reputation: 33
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
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
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