'************************************************
'** 函数名称: GetGivenStr
'** 函数功能: 取指定范围内的字符串
'** 参数说明:
'** strSource 为源字符串,即分解前的字符串
'** strStartKey 是指开始字符串
'** strEndKey 是指结束字符串
'** lngDefStart 指默认开始位置
'** 函数返回:
'** String 类型
'** "" 找不到指定的字符串
'** 一个字符串 第一次找到的符合条件的字符串
'** 参考实例:
'** 如 strTemp = "我的朋友[001]其他字符"
'** 现在要将 001 取出来,那么就可以用
'** strReturn = GetGivenStr(strTemp, "[", "]")
'************************************************
Public Function GetGivenStr(ByVal strSource As String, _
ByVal strStartKey As String, _
ByVal strEndKey As String, _
Optional ByVal lngDefStart As Long = 1) As String
Dim lngStart As Long
Dim lngEnd As Long
'// 求出出现开始字符串和结束字符串的位置
lngStart = InStr(lngDefStart, strSource, strStartKey)
'// 如果未找到这个字符,则退出此函数
If lngStart = 0 Then
GetGivenStr = ""
Exit Function
End If
'// 将起始位置加上开始找的字符串的长度,如 strStartKey="(" ,那么就加1
lngStart = lngStart + Len(strStartKey)
'// 查找结束字符串的位置
lngEnd = InStr(lngStart, strSource, strEndKey)
'// 如果两者有一个没有找到,则退出函数
If lngStart = 0 Or lngEnd = 0 Then
GetGivenStr = ""
Exit Function
End If
'// 取得指定范围的字符
GetGivenStr = Mid$(strSource, lngStart, lngEnd - lngStart)
End Function