开发者

Need help is getting the substring

I am having string in format

"<testname>$ns1$,$NS2$,$NS3$</testname>"

and i need to get the string as "$ns1$,$NS2$,$NS3$"

there may be possibility of geeting string as

"    <testname>$ns1$,$NS2$,$NS3$</testname>"
开发者_如何转开发

Thanks in advance Excel Dev


So to reformulate your problem, you want to cut out a substring starting after the first ">" and ending before the first "

Before we go to VBA let's try to find a formula

cell [A1] contains text "<tag>Content</tag>"

so

   Formula                            Result      Comment
1) =FIND(">",A1,1)+1                  6           1st char of inner
2) =FIND("</",A1)-1                   12          last char of inner
3) =FIND("</",A1)-FIND(">",A1,1)-1    7           length of inner

combining 1 & 3 you get

4) =MID(A1,FIND(">",A1,1)+1,FIND("</",A1)-FIND(">",A1;1)-1)
                                      Content     the inner

meaning that you can cut out the inner without VBA ..... but we sure can provide brilliant VBA as well :-))

Hope that helps


Function GetInnerTag(byval value as string) as string
   dim offset1 as integer
   dim offset2 as integer
   offset1= instr(1,value,">")
   'If the index of the first greater-than
   'is less than one, return an empty string
   if offset1 <1 then
      GetInnerTag=""
      Exit Function
   end if

   offset2=instr(offset1+1,value,"<")

   'If the index of the first less-than 
   'less than one, return ""
   if offset2 <1 then
      GetInnerTag=""
      Exit Function
   end if
   GetInnerTag=mid(value,offset1+1,(offset2-offset1-1))
End Function
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜