Removing a specific character to the right and left of a line in vb6 (TrimChar)

I want to remove some specific characters that were misused in a string.

"........ I.wanna.delete.only.the.dots.outside.of.this.text ..............." p>

As you can see, I cannot use a replacement for this. I have to find a way to remove only the characters to the left and right of the line, and these points are just an example of the characters that I want to delete. I have an array of these unwanted characters. So, after the process line should look like

"I.wanna.delete.only.the.dots.outside.of.this.text"

but I could not find a way to get this job.

+4
source share
2 answers

VB6 has a Trim() function, but only removes spaces.

To remove characters from both ends, you need to check each end, in turn, delete 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 optimized, but you can expand it to just work out the end positions, and then make one call to Mid() .

+12
source
  Public Function Remove(text As String) As String text = Replace(text, "..", "") If Left(text, 1) = "." Then text = Right(text, Len(text) - 1) End If If Right(text, 1) = "." Then text = Left(text, Len(text) - 1) End If Remove = text End Function 

If you delete all instances of "..", you may or may not leave one leading point and one end point for processing. If you are lucky to guarantee that the points will always be an even number, then

  text = Replace(text, "..", "") 

- thatโ€™s all you need.

+1
source

Source: https://habr.com/ru/post/1386663/


All Articles