Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deleting certain character from right and left of a string in vb6 (TrimChar)

Tags:

string

vb6

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.

like image 246
Berker Yüceer Avatar asked Dec 13 '22 07:12

Berker Yüceer


1 Answers

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.

like image 188
Deanna Avatar answered May 19 '23 06:05

Deanna