我写了一个简单的代码,下面是进程字符串值“words”,如果单词有一个前导单引号(例如:'em)或尾随一个(例如:tryin')或两者(例如:'this')然后它会删除它们,如果有多个标记实例也会被删除。但如果引号是中间的,请不要管它,例子(美国' s,isn' t,can')
如何实现相同的功能不同和有效?而且我不想使用递归过程来执行此操作。
Sub RemoveSurroundingSingleQuotes()
Dim arr(), wrd$
arr = Array("'wrd1'", "''wrd3''")
For i = 0 To UBound(arr)
wrd = arr(i)
Debug.Print "word before: " & wrd
RemoveLeft:
If Left(wrd, 1) = "'" Then wrd = Mid(wrd, 2)
'check the left side again
If Left(wrd, 1) = "'" Then GoTo RemoveLeft:
RemoveRight:
If Right(wrd, 1) = "'" Then wrd = Mid(wrd, 1, Len(wrd) - 1)
'check the Right side again
If Right(wrd, 1) = "'" Then GoTo RemoveRight:
Debug.Print "word after: " & wrd & vbCr & "--------------"
Next
End Sub
答案 0 :(得分:1)
这里有一些代码使用While循环来查找起始位置和结束位置,所以它只知道字符串重新分配一次,知道起始位置和结束位置之后。
forceLinkClasses
答案 1 :(得分:0)
Private Function Dequoted(S As String) As String
If Left(S, 1) = """" And Right(S, 1) = """" Then Dequoted = Mid(S, 2, Len(S) - 2) Else Dequoted = S
End Function