| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 383 人关注过本帖
标题:非常高效方便的Mid函数扩展版
只看楼主 加入收藏
VB爱上我
Rank: 6Rank: 6
等 级:贵宾
威 望:25
帖 子:478
专家分:52
注 册:2005-10-14
结帖率:100%
收藏
 问题点数:0 回复次数:0 
非常高效方便的Mid函数扩展版
操作字符串是我们平时经常碰到的,比如做采集工具时,我们要对一大串字符进行分析,取出有用的部分。但是用vb自带的字符串函数需要写很多代码才能获取到,而且这种操作很频繁使用,所以必须写一个高效方便的函数。代码写的不好之处请高手指出,看是否还有更好的方法!

<%
'------------------------------------------------------------
'获取源字符串中以某字符串开始并且以某字符串结束,中间的那段字符串
'SourceStr : 源字符串
'BeginStr : 开始字符串
'EndStr : 结束字符串
'WithB : 是否包含开始字符串(1代表包含0代表不包含)
'WithE : 是否包含结束字符串(1代表包含0代表不包含)
'说明:开始字符串一定要保证是唯一字符串,否则会是在源字符串中第一次出现的那个位置
'-----------------------------------------------------------------------------
function GetMidWith(byval SourceStr,BeginStr,EndStr,WithB,WithE)
 dim bIndex

 bIndex = instr(SourceStr,BeginStr)
 if bIndex <= 0 then GetMidWith = "":exit function
 
 SourceStr = mid(SourceStr,bIndex + len(BeginStr))'不包含开始字符串
 
 bIndex = instr(SourceStr,EndStr)
 if bIndex <= 0 then GetMidWith = SourceStr:exit function

 if WithE = 1 then
  GetMidWith = mid(SourceStr,1,bIndex + len(EndStr) - 1)'包含结束字符串
 else
  GetMidWith = mid(SourceStr,1,bIndex-1)'不包含结束字符串
 end if
 
 
 if WithB = 1 then GetMidWith = BeginStr & GetMidWith'包含开始字符串
end function

'------------------------------------------------------------
'获取源字符串中以某字符串开始并且以某字符串结束,中间的那段字符串
'SourceStr : 源字符串
'BeginStr : 开始字符串
'EndStr : 结束字符串
'WithB : 是否包含开始字符串(1代表包含0代表不包含)
'WithE : 是否包含结束字符串(1代表包含0代表不包含)
'说明:开始字符串与结束字符串一定要保证是唯一字符串
'-----------------------------------------------------------------------------
function GetMid(byval SourceStr,BeginStr,EndStr,WithB,WithE)
 dim bIndex
 dim eIndex

 bIndex = instr(SourceStr,BeginStr)
 eIndex = instr(SourceStr,EndStr)
 if bIndex <= 0 or eIndex <= 0 or eIndex - bIndex<=0 then GetMid = "":exit function
 
 if WithB = 1 then
  if WithE = 1 then
   GetMid = mid(SourceStr,bIndex,eIndex + len(EndStr) - bIndex)'包含开始字符串 包含结束字符串
  else
   GetMid = mid(SourceStr,bIndex,eIndex - bIndex)'包含开始字符串 不包含结束字符串
  end if
 else
  if WithE = 1 then
   GetMid = mid(SourceStr,bIndex + len(BeginStr),eIndex + len(EndStr) - bIndex - len(BeginStr))'不包含开始字符串 包含结束字符串
  else
   GetMid = mid(SourceStr,bIndex + len(BeginStr),eIndex - len(BeginStr) - bIndex)'不包含开始字符串 不包含结束字符串
  end if
 end if
end function

'测试函数的功能
dim sstr
sstr = "[x]my name is dujun.[/x][x][x]nice to meet you![/x]"

response.write sstr & "<br><br>"
response.write "GetMid <span style=""color:blue"">[x]</span> <span style=""color:red"">![/x]</span>" & "<br><br>"

response.write GetMid(sstr,"[x]","![/x]",0,0) & "<span style=""padding-left:20px;color:red"">00</span><br>"
response.write GetMid(sstr,"[x]","![/x]",0,1) & "<span style=""padding-left:20px;color:red"">01</span><br>"
response.write GetMid(sstr,"[x]","![/x]",1,0) & "<span style=""padding-left:20px;color:red"">10</span><br>"
response.write GetMid(sstr,"[x]","![/x]",1,1) & "<span style=""padding-left:20px;color:red"">11</span><br>"

response.write "<br><br>"
response.write "GetMidWith <span style=""color:blue"">[x]</span> <span style=""color:red"">[/x]</span>" & "<br><br>"

response.write GetMidWith(sstr,"[x]","[x]",0,0) & "<span style=""padding-left:20px;color:red"">00</span><br>"
response.write GetMidWith(sstr,"[x]","[x]",0,1) & "<span style=""padding-left:20px;color:red"">01</span><br>"
response.write GetMidWith(sstr,"[x]","[x]",1,0) & "<span style=""padding-left:20px;color:red"">10</span><br>"
response.write GetMidWith(sstr,"[x]","[x]",1,1) & "<span style=""padding-left:20px;color:red"">11</span><br>"
%>

原文:http://
搜索更多相关主题的帖子: 分析 字符串 而且 
2011-10-29 22:01
快速回复:非常高效方便的Mid函数扩展版
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015057 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved