一个较复杂的字符串截取函数

来源:风之相随'S BLOG 时间:2009-11-02 10:45:00 

这个函数是前几年刚流行小偷程序的时候,偶写来用于小偷程序中截取代码的;

可能有些朋友在我以前的代码中看见过了,但没有写用法,现在把调用方法及使用示例写出来。

也许对有些朋友比较有用吧。


'    Function(公有)
'    名称 :    盛飞字符串截取函数
'    作用 :    按指定首尾字符串截取内容(本函数为从左向右截取)
'    参数 :    sContent ---- 被截取的内容
'        sStart ------ 首字符串
'        iStartNo ---- 当首字符串不是唯一时取第几个
'        bIncStart --- 是否包含首字符串(1/True为包含,0/False为不包含)
'        iStartCusor - 首偏移值(指针单位为字符数量,左偏用负值,右偏用正值,不偏为0)
'        sOver ------- 尾字符串
'        iOverNo ----- 当尾字符串不是唯一时取第几个
'        bIncOver ---- 是否包含尾字符串((1/True为包含,0/False为不包含)
'        iOverCusor -- 尾偏移值(指针单位为字符数量,左偏用负值,右偏用正值,不偏为0)

Public Function SenFe_Cut(sContent, sStart, iStartNo, bIncStart, iStartCusor, sOver, iOverNo, bIncOver, iOverCusor)
    If sContent<>"" Then
        Dim iStartLen, iOverLen, iStart, iOver, iStartCount, iOverCount, I
        iStartLen = Len(sStart)    '首字符串长度
        iOverLen  = Len(sOver)    '尾字符串长度
        '首字符串第一次出现的位置
        iStart = InStr(sContent, sStart)
        '尾字符串在首字符串的右边第一次出现的位置
        iOver = InStr(iStart + iStartLen, sContent, sOver)
        If iStart>0 And iOver>0 Then
            If iStartNo < 1 or IsNumeric(iStartNo)=False Then iStartNo = 1
            If iOverNo < 1 or IsNumeric(iOverNo)=False Then iOverNo  = 1
            '取得首字符串出现的次数
            iStartCount = UBound(Split(sContent, sStart))
            If iStartNo>1 And iStartCount>0 Then
                If iStartNo>iStartCount Then iStartNo = iStartCount
                For I = 1 To iStartNo
                    iStart = InStr(iStart, sContent, sStart) + iStartLen
                Next
                iOver = InStr(iStart, sContent, sOver)
                iStart = iStart - iStartLen    '还原默认状态:包含首字符串
            End If
            '取得尾字符串出现的次数
            iOverCount = UBound(Split(Mid(sContent, iStart + iStartLen), sOver))
            If iOverNo>1 And iOverCount>0 Then
                If iOverNo>iOverCount Then iOverNo = iOverCount
                For I=1 To iOverNo
                    iOver = InStr(iOver, sContent, sOver) + iOverLen
                Next
                iOver = iOver - iOverLen    '还原默认状态:不包含尾字符串
            End If
            If CBool(bIncStart)=False Then iStart = iStart + iStartLen    '不包含首字符串
            If CBool(bIncOver)  Then iOver = iOver + iOverLen        '包含尾字符串
            iStart = iStart + iStartCusor    '加上首偏移值
            iOver  = iOver + iOverCusor    '加上尾偏移值
            If iStart<1 Then iStart = 1
            If iOver<=iStart Then iOver = iStart + 1
            '按指定的开始和结束位置截取内容
            SenFe_Cut = Mid(sContent, iStart, iOver - iStart)
        Else
            'SenFe_Cut = sContent
            SenFe_Cut = "没有找到您想要的内容,可能您设定的首尾字符串不存在!"
        End If
    Else
        SenFe_Cut = "没有内容!"
    End If
End Function

使用示例:


另外发一个稍简单的截取函数


'    Function(公有)
'    名称 :    盛飞字符串截取函数
'    作用 :    按指定首尾字符串截取内容(本函数为从左向右截取)
'    参数 :    sContent ---- 被截取的内容
'        sStart ------ 首字符串
'        sOver ------- 尾字符串
'        iType ------- 类型(1:不包含首尾字符串;2:包含首尾字符串;3:包含首、不包含尾;4:不包含首、包含尾)

Function SenFe_Cut(sContent, sStart, sOver, iType)
    Dim iStart, iOver
    iStart = InStr(sContent,sStart)
    iOver  = InStr(iStart+Len(sStar),sContent,sOver)
    If iStart>0 And iOver>0 Then
        Select Case iType
            Case 1 iStart = iStart+Len(sStart)
            Case 2 iOver  = iOver+Len(sOver)
            Case 4 iStart = iStart+Len(sStart):iOver = iOver+Len(sOver)
        End Select
        SenFe_Cut = Mid(sContent,iStart,iOver-iStart)
    Else
        SenFe_Cut = "没有找到您想要的内容,可能您设定的首尾字符串不存在!"
    End If
End Function另外也可以用正则截取,代码更简单,就不用贴了。

标签:字符串,函数,截取
0
投稿

猜你喜欢

  • 详解Mysql命令大全(推荐)

    2024-01-20 08:00:35
  • ASP字符串16进制互转

    2008-06-24 12:28:00
  • Python异常模块traceback用法实例分析

    2022-07-22 15:47:14
  • 通过实例解析js简易模块加载器

    2024-04-16 08:55:57
  • Python 类方法和实例方法(@classmethod),静态方法(@staticmethod)原理与用法分析

    2023-09-16 04:58:30
  • 利用二进制文件安装etcd的教程详解

    2023-07-22 00:23:47
  • 浅谈python中频繁的print到底能浪费多长时间

    2022-01-31 06:24:34
  • 关于PySnooper 永远不要使用print进行调试的问题

    2022-12-08 10:29:35
  • 使用canal监控mysql数据库实现elasticsearch索引实时更新问题

    2024-01-20 22:48:39
  • 数据库触发器(Trigger)的一点使用心得

    2024-01-28 14:21:29
  • Python实现处理管道的方法

    2022-05-17 22:58:29
  • python3中的logging记录日志实现过程及封装成类的操作

    2023-07-30 21:58:21
  • python TK库简单应用(实时显示子进程输出)

    2023-10-08 23:08:19
  • postman批量执行接口测试的图文步骤

    2023-03-14 00:22:13
  • 解决IOS端微信H5页面软键盘弹起后页面下方留白的问题

    2024-04-27 15:47:30
  • sql2005 安装教程 图文

    2024-01-16 23:47:51
  • go语言Pflag Viper Cobra 核心功能使用介绍

    2024-04-25 15:26:28
  • 高手进阶:网页设计中的文字运用

    2008-10-05 08:58:00
  • 基于python写个国庆假期倒计时程序

    2022-06-04 20:35:32
  • Python reversed函数及使用方法解析

    2023-01-24 16:10:53
  • asp之家 网络编程 m.aspxhome.com