取指定范围内的字符串

'************************************************
'** 函数名称:  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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值