繁体   English   中英

VBscript和InStr函数

[英]VBscript and InStr function

我正在尝试检索彭赞斯海盗少将歌曲“海盗”中字符串“少将”之前和之后的5个字符(位于: http : //www.naic.edu/~gibson/poems/gilbert1.html )。 我正在尝试找到一种比我现有的方法更好的方法,并且试图弄清为什么它没有循环。 任何想法将不胜感激!

l1 =INSTR(l2, string, "Major-General")
l2 = 5
l3 = 1
vcount=0

if vcount <5 then
    l1 =INSTR(l3, string, "Major-General")
    vcount = vcount +1 
    word = mid(string, l1-5, l2 )
    word1 = mid(string, l1+13, l2)
    l3 = l3+l1
    response.write "<br>" & "5 before: " & word & "<br>"  & "5 after: " & word1 
end if

这样做可能会更容易:

Dim Poem,aStr,i,block,Left5,Right5

Poem = "The abc poem is abc in this abc string."

aStr = Split(Poem,"abc")

If uBound(aStr) = 0 Then Response.Write "String not found": Response.End

For i = 0 to uBound(aStr)
  block = Trim(aStr(i))
  Left5 = "": Right5 = ""
  if cStr(i) <> cStr(uBound(aStr)) then Left5 = Right(block,5)
  If cStr(i) <> cStr(0) then Right5 = Left(block,5)

  Response.Write "The 5 characters to the left of abc:" & Left5
  Response.Write "The 5 characters to the right of abc:" & Right5

Next

每次字符串存在时,它将在字符串的左侧和右侧获得5个字符。

在这种情况下,它将在Poem字符串中搜索abc并将该字符串拆分为该字符串所在的每个部分的数组,然后可以对其进行进一步处理。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM