在vb6(TrimChar)中从字符串的左右删除某些字符 [英] Deleting certain character from right and left of a string in vb6 (TrimChar)

查看:33
本文介绍了在vb6(TrimChar)中从字符串的左右删除某些字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想删除某些在字符串中错误使用的字符.

I want to delete some certain characters that used wrongly in a string.

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

"........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

我想删除.only.the.dots.outside.of.this.text"

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

但我找不到获得这项工作的方法.

but I couldn't find a way to get this work.

推荐答案

VB6 有一个 Trim() 函数,但它只删除空格.

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

结果:

?TrimChar("........I.wanna.delete.only.the.dots.outside.of.this.text...............", ".")
I.wanna.delete.only.the.dots.outside.of.this.text

这还远未优化,但您可以对其进行扩展以计算出结束位置,然后执行单个 Mid() 调用.

This is far from optimised but you could expand on it to just work out the end positions, then do a single Mid() call.

这篇关于在vb6(TrimChar)中从字符串的左右删除某些字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆