I want to delete some certain characters that used wrongly in a string.
"........I.wanna.delete.only.the.dots.outside.of.this.text..............."
as you can see I can't use replace for this. I must find a way to delete only the characters at left and right of a string and those dots only an example of characters that I want to delete. I have an array of those unwanted characters. So after the process string should look like
"I.wanna.delete.only.the.dots.outside.of.this.text"
but I couldn't find a way to get this work.
VB6 has a Trim()
function but that only removes spaces.
To remove characters from both ends, you'll need to check each end in turn removing the character until you get something else:
Function TrimChar(ByVal Text As String, ByVal Characters As String) As String
'Trim the right
Do While Right(Text, 1) Like "[" & Characters & "]"
Text = Left(Text, Len(Text) - 1)
Loop
'Trim the left
Do While Left(Text, 1) Like "[" & Characters & "]"
Text = Mid(Text, 2)
Loop
'Return the result
TrimChar = Text
End Function
Result:
?TrimChar("........I.wanna.delete.only.the.dots.outside.of.this.text...............", ".")
I.wanna.delete.only.the.dots.outside.of.this.text
This is far from optimised but you could expand on it to just work out the end positions, then do a single Mid()
call.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With