替换 ' 时堆栈溢出 与'' 在VB 6.0中
我正在研究一些旧版 VB 6.0 代码(Access XP 应用程序)来解决 Access 应用程序的 SQL 语句问题。 对于客户名称中包含撇号的情况,我需要使用用 2 个单引号替换单引号(例如“医生的手术”:
Replace(customerName, "'", "''")
这将转义单引号,因此我得到有效的 SQL:
SELECT blah FROM blah WHERE customer = 'Doctor''s Surgery'
不幸的是,替换函数会导致无限循环和堆栈溢出,大概是因为它替换函数递归地将每个添加的引号转换为另外 2 个引号,例如,一个引号被替换为两个,然后第二个引号也被替换为两个,依此类推...
----- -----------编辑--------------
我注意到(感谢海报)该项目中使用的替换函数是自定义编写的:
Public Function replace(ByVal StringToSearch As String, ByVal ToLookFor As String,
ByVal ToReplaceWith As String) As String
Dim found As Boolean
Dim position As Integer
Dim result As String
position = 0
position = InStr(StringToSearch, ToLookFor)
If position = 0 Then
found = False
replace = StringToSearch
Exit Function
Else
result = Left(StringToSearch, position - 1)
result = result & ToReplaceWith
result = result & Right(StringToSearch, Len(StringToSearch) - position - Len(ToLookFor) + 1)
result = replace(result, ToLookFor, ToReplaceWith)
End If
replace = result
End Function
显然, VB 并不总是有自己的替换功能。这个实现一定是有缺陷的。我将遵循人们的建议并删除它以支持 VB 6 的实现 - 如果这不起作用,我将编写自己的可行的功能。谢谢各位的意见!
I'm looking into some legacy VB 6.0 code (an Access XP application) to solve a problem with a SQL statement by the Access app. I need to use replace single quotes with 2 single quotes for cases where a customer name has an apostrophe in the name (e.g. "Doctor's Surgery":
Replace(customerName, "'", "''")
Which will escape the single quote, so I get the valid SQL:
SELECT blah FROM blah WHERE customer = 'Doctor''s Surgery'
Unfortunately the Replace function causes an infinite loop and stack overflow, presumably because it replace function recursively converts each added quote with another 2 quotes. E.g. one quote is replaced by two, then that second quote is also replaced by two, and so on...
----------------EDIT---------------
I have noticed (thanks to posters) that the replace function used in this project is custom-written:
Public Function replace(ByVal StringToSearch As String, ByVal ToLookFor As String,
ByVal ToReplaceWith As String) As String
Dim found As Boolean
Dim position As Integer
Dim result As String
position = 0
position = InStr(StringToSearch, ToLookFor)
If position = 0 Then
found = False
replace = StringToSearch
Exit Function
Else
result = Left(StringToSearch, position - 1)
result = result & ToReplaceWith
result = result & Right(StringToSearch, Len(StringToSearch) - position - Len(ToLookFor) + 1)
result = replace(result, ToLookFor, ToReplaceWith)
End If
replace = result
End Function
Apparently, VB didn't always have a replace function of it's own. This implementation must be flawed. An going to follow folk's advice and remove it in favour of VB 6's implementation - if this doesn't work, I will write my own which works. Thanks everyone for your input!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您确定它不是替换功能的专有实现吗?
如果是的话用VB6的Replace替换即可。
我不记得它出现在哪个版本中(不是在 Vb3 中,而是在 VB6 中),因此如果原始代码库是 vb3/4,那么它可能是手动编码版本。
编辑
我刚刚看到你的编辑,我是对的!
是的,您应该能够删除该函数,然后它将使用内置 VB6 替换函数。
Are you sure that it's not a proprietary implementation of the Replace function?
If so it can just be replaced by VB6's Replace.
I can't remember which version it appeared in (it wasn't in Vb3, but was in VB6) so if the original code base was vb3/4 it could be a hand coded version.
EDIT
I just saw your edit, I was Right!
Yes, you should be able to just remove that function, it'll then use the in build VB6 replace function.
我们使用的 VB6 应用程序可以选择将 ' 替换为 ` 或完全删除它们。
您还可以遍历这些字母,构建第二个字符串并将每个“作为”插入。
We use an VB6 application that has the option of replacing ' with ` or removing them completely.
You could also walk through the letters, building a second string and inserting each ' as ''.
我刚刚在 Access 中尝试过,它工作正常(没有 stackoverflow):
I just tried this in Access and it works fine (no stackoverflow):