首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >VBscript和InStr函数

VBscript和InStr函数
EN

Stack Overflow用户
提问于 2014-09-25 16:30:40
回答 1查看 628关注 0票数 0

我正在尝试检索潘赞斯大将军的海盗中的字符串“大将军”前后的5个字符(在这里找到:http://www.naic.edu/~gibson/poems/gilbert1.html )。我试图找到一种比我所拥有的更好的方法来做这件事,并试图找出为什么它不是循环的。任何想法都将不胜感激!

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-09-25 16:58:03

这样做可能更容易:

代码语言:javascript
复制
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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26043640

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档