注册 登录
编程论坛 VB6论坛

求一个RFIND函数的每句什么意思

natesc 发布于 2017-02-03 17:06, 1977 次点击
程序代码:
Public Function rFind(ByVal txt As String, ByVal code1 As String, Optional ByVal code2 As String = """", Optional ByVal rev As Boolean = False)
    On Error Resume Next
    Dim tmp As String, i1 As Long, i2 As Long
    tmp = txt
    If InStr(tmp, code1) = 0 Then rFind = "": Exit Function
    i1 = InStr(tmp, code1) + Len(code1)
    tmp = Mid(tmp, i1)
    If InStr(tmp, code2) = 0 Then rFind = "": Exit Function
    If rev Then
       i2 = InStrRev(tmp, code2)
    Else
       i2 = InStr(tmp, code2)
    End If
    rFind = Mid(tmp, 1, i2 - 1)
End Function

特别是其中I1,I2,TMP,COD1,COD2,及最后的RFIND都是什么意思[color=#0000FF][/color]

[此贴子已经被作者于2017-2-3 17:18编辑过]

5 回复
#2
风吹过b2017-02-03 17:53
Public Function rFind(ByVal txt As String, ByVal code1 As String, Optional ByVal code2 As String = """", Optional ByVal rev As Boolean = False)
    On Error Resume Next
    Dim tmp As String, i1 As Long, i2 As Long
    tmp = txt           '取传入的字串
    If InStr(tmp, code1) = 0 Then rFind = "": Exit Function     '如果转入的字串不包含第一个字串,返回空,结束函数
    i1 = InStr(tmp, code1) + Len(code1)           '计算第一个字串在目标字串结束的位置
    tmp = Mid(tmp, i1)           '取第一个字串在目标字串之后的字符
    If InStr(tmp, code2) = 0 Then rFind = "": Exit Function       '如果不包含第二个字串,返回空,结束函数
    If rev Then                 '传入第二个字串查找方向
       i2 = InStrRev(tmp, code2)       '从右向左查找
    Else
       i2 = InStr(tmp, code2)         '从左到右查找
    End If
    rFind = Mid(tmp, 1, i2 - 1)       '得到从第一字串结束到 第二个字串开始之间的字符,并返回结果
                                      'rFind 是函数名,可以理解为一个变量,该变量用于返回函数结果,只能写在 等号的左边
                                      '如果写在右边就是递归调用
End Function
#3
natesc2017-02-03 18:44
谢谢指教
#4
natesc2017-02-03 18:46
回复 3楼 natesc
那么在下面这代码中引用,又怎么理解
程序代码:
Public Function RichStr(TMRichText As String, xxRichText As String) As String
   
    If TMRichText = "" Then
        RichStr = ""
        Exit Function
    End If
   
    If xxRichText = "" Then
        RichStr = ""
        Exit Function
    End If
   
   
    Dim TMlen As Long
    Dim XXlen As Long
   
    Dim TMStra As String
    Dim XXStr As String
   
    TMlen = Val(rFind(TMRichText, "lang2052\f0\fs", " "))
    XXlen = Val(rFind(xxRichText, "lang2052\f0\fs", " "))
   
    TMStra = rFind(TMRichText, "lang2052\f0\fs" & TMlen, "\par")
    XXStr = rFind(xxRichText, "lang2052\f0\fs" & XXlen, "\par")
   
   
    '开始拼接编码
   
    Dim RichStr1 As String
   
    RichStr1 = TMStra & "\par  \par" & XXStr
   
    Dim Str1 As String
   
    Str1 = "{\rtf1\ansi\ansicpg936\deff0\deflang1033\deflangfe2052{\fonttbl{\f0\fnil\fcharset134 \'cb\'ce\'cc\'e5;}}" & vbCrLf & "\viewkind4\uc1\pard\lang2052\f0\fs" & TMlen + XXSStr & RichStr1 & "\par }"
   
    RichStr = Str1
   
   
End Function


[此贴子已经被作者于2017-2-3 18:47编辑过]

#5
natesc2017-02-03 21:14
回复 2楼 风吹过b
Public Function rFind(ByVal txt As String, ByVal code1 As String, Optional ByVal code2 As String = """", Optional ByVal rev As Boolean = False)
    On Error Resume Next
    Dim tmp As String, i1 As Long, i2 As Long
    tmp = txt                                         'tmp赋值txt
    If InStr(tmp, code1) = 0 Then rFind = "": Exit Function    '如果tmp为零长度或者txt中没有子串code1,rFind = ""同时退出函数
    i1 = InStr(tmp, code1) + Len(code1)               '定位txt中包含的第一个子串code1之后
    tmp = Mid(tmp, i1)                                '从上述位置截取tmp重新赋值个tmp
    If InStr(tmp, code2) = 0 Then rFind = "": Exit Function    '如果tmp为零长度或者txt中没有子串code2,rFind = ""同时退出函数
    If rev Then
        i2 = InStrRev(tmp, code2)                     '如果反序,则用InStrRev求子串code2在tmp的位置
    Else
        i2 = InStr(tmp, code2)                        '如果不是反序,则用InStr求子串code2在tmp的位置
    End If
    rFind = Mid(tmp, 1, i2 - 1)                       '从上述位置截取tmp重新赋值个tmp,只有tmp中有2个或2个以上的子串code2时,反序和正序才有差别,当只有一个时,逆序正序结果相同
End Function
这是另一个网友给的解释,不知道有什么不同
#6
xiangyue05102017-02-04 08:41
风版真的很热心,也有耐心。
学编程就是读代码到写代码的过程,像这种自己不去读代码,所有的语句都要别人替他标注的,我说实话不爱搭理。
1