非常高效方便的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://