[英]How to remove the last occurrence of a character within a string?
我正在编写一个动态编写sql-query的函数来检索一些帖子,我遇到了一个小问题。
伪代码:
if trim$(sSqlQuery) <> "" then
sSqlQuery = "foo foo ) foo"
end if
if 1 = 1 then
sSqlQuery = sSqlQuery & "bar bar bar"
end if
此函数在大多数情况下返回正确的sql-query,但由于此前的早期函数中的某些情况,将触发第二个if子句。 导致奇怪的查询结果。
我需要做的是弄清楚如何在sSqlQuery中删除最后一次出现的“)”,然后将第二组查询附加到第二个if子句中的总查询。
在伪我认为它看起来像这样:
if 1 = 1 then
call removeLastOccurringStringFromString(sSqlQuery, ")")
sSqlQuery = sSqlQuery & "bar bar bar"
end if
但是,我发现很难掌握Right()
Left()
和Mid()
函数。
我试过的是这个:
nLen = InStrRev(sSqlSokUrval, ")") ' To get the positional value of the last ")"
在那之后,我完全迷失了。 因为如果我用Mid()
子串起来,我只会得到“)”而没有别的。
任何关于如何解决这个问题的想法和/或暗示都将受到高度赞赏! 谢谢!
'Searches subject and removes last (and *only* last) occurence of the findstring
Function RemoveLastOccurenceOf(subject, findstring)
Dim pos
'Find last occurence of findstring
pos = InstrRev(subject, findstring, -1, vbBinaryCompare) 'use vbTextCompare for case-INsensitive search
if pos>0 then 'Found it?
'Take left of subject UP UNTIL the point where it was found
'...Skip length of findstring
'...Add rest of string
RemoveLastOccurenceOf = Left(subject, pos - 1) & Mid(subject, pos + len(findstring))
else 'Nope
'Return entire subject
RemoveLastOccurenceOf = subject
end if
End Function
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.