2014-09-25 71 views
0

我试图检索海盗的Penzance少将歌(在这里找到:http://www.naic.edu/~gibson/poems/gilbert1.html)字符串“少将”之前和之后的5个字符。我试图找到一个比我有更好的方式来做到这一点,并试图找出为什么它不循环。任何想法将不胜感激!VBscript和InStr函数

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 
+3

_“试图找出它为什么不循环”_好,因为实际上我没有看到该代码中的任何循环... – 2014-09-25 16:36:31

+0

不够公平。这样做比使用mid()更好吗?它看起来笨重。 – 2014-09-25 16:55:25

回答

0

它可能会更容易做到这一点是这样的:

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并将字符串拆分为字符串存在的每个部分的数组,然后可以进一步处理它。

+0

非常感谢! – 2014-09-25 16:58:55

+0

不客气。 – 2014-09-25 17:01:22