代码之家  ›  专栏  ›  技术社区  ›  James Allen

在VB 6.0中用“”替换“”时堆栈溢出

  •  2
  • James Allen  · 技术社区  · 17 年前

    我正在研究一些遗留的VB6.0代码(Access XP应用程序),以解决Access应用程序的SQL语句问题。对于客户名称中有撇号的情况(例如“医生的手术”),我需要使用2个单引号替换单引号:

    Replace(customerName, "'", "''")
    

    SELECT blah FROM blah WHERE customer = 'Doctor''s Surgery'
    

    不幸的是,Replace函数会导致无限循环和堆栈溢出,可能是因为Replace函数递归地将每个添加的引号转换为另外两个引号。例如,一个引用被两个替换,然后第二个引用也被两个替换,依此类推。。。

    ----------------编辑---------------

    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并不总是有自己的替换函数。这种实现肯定有缺陷。我将遵循folk的建议,将其删除,以支持VB6的实现——如果这不起作用,我将编写我自己的,有效的。谢谢大家的意见!

    3 回复  |  直到 17 年前
        1
  •  8
  •   Binary Worrier    17 年前

    您确定它不是Replace函数的专有实现吗?

    如果是这样,可以用VB6的Replace替换它。

    我不记得它出现在哪个版本(不是在Vb3中,而是在VB6中),所以如果原始代码库是Vb3/4,它可能是手工编码的版本。

    编辑

    是的,您应该能够删除该函数,然后它将使用内置VB6替换函数。

        2
  •  2
  •   cjk    17 年前

    我们使用的VB6应用程序可以选择将“替换为”或完全删除它们。

        3
  •  2
  •   Mitch Wheat    17 年前

     Public Function ReplaceSingleQuote(tst As String) As String
            ReplaceSingleQuote = Replace(tst, "'", "''")
     End Function
    
    
     Public Sub TestReplaceSingleQuote()
            Debug.Print ReplaceSingleQuote("Doctor's Surgery")
     End Sub