|
最近想做一个截取两个字符之间的字符串的函数对于第二个函数
Public Function interceptStrinTwoChars(findChar1 As String, seq1 As Integer, findchar2 As String, seq2 As Integer, withinStr As String, Optional trimFlag As Boolean = True) As String
想把seq1和seq2改成默认为1,如下所示会报错
Public Function interceptStrinTwoChars(findChar1 As String, Optional seq1 As Integer = 1,findchar2 as String,optional seq2 as Integer =1,withinstr as String,optional trimFlag as Boolean=True) as String 能帮忙看下错在哪里了么
'得到一个字符串中第n个字符所在的位置的位置
'比如说要得到第二个空格的位置myInstr(" ",2,"how are you",)
'比如说要得到倒数第二个空格的位置 myInstr("",-2,"how are you ")
Public Function myInstr(findstr As String, n As Integer, withinStr As String) As Integer
Dim i, m As Integer
If n > 0 Then
m = InStr(withinStr, findstr)
While n > 1
m = InStr(m + 1, withinStr, findChar)
n = n - 1
Wend
Else
m = InStrRev(withinStr, findstr)
While n < -1
m = InStrRev(withinStr, findstr, m - 1)
n = n + 1
Wend
End If
myInstr = m
End Function
Public Function interceptStrinTwoChars(findChar1 As String, seq1 As Integer, findchar2 As String, seq2 As Integer, withinStr As String, Optional trimFlag As Boolean = True) As String
Dim start, lenth As Integer
start = myInstr(findChar1, seq1, withinStr) + 1
lenth = myInstr(findchar2, seq2, withinStr) - myInstr(findChar1, seq1, withinStr) - 1
If trimFlag = True Then
interceptStrinTwoChars = Trim(Mid(withinStr, start, lenth))
Else
interceptStrinTwoChars = Mid(withinStr, start, lenth)
End If
End Function
|
|