VB截取字符串

</crop>
- <crop id="402" offsetX="0" offsetY="0" seed="Seed" harvestNum="3" isAnimation="0">
<insect position="0,0,0,0,0,0|67,-9,85,-39,111,-19|61,-46,83,-71,117,-46|0,0,0,0,0,0" />
<cropGrow value="21600,43200,64800,86400,108000,2000000000" />
<tip>12:</tip>
<name>二制服</name>
<gossip />
</crop>
- <crop id="403" offsetX="0" offsetY="0" seed="Seed" harvestNum="5" isAnimation="0">
<insect position="0,0,0,0,0,0|67,-9,93,-45,102,-21|78,6,91,-18,104,5|0,0,0,0,0,0" />
<cropGrow value="28800,61200,93600,129600,165600,2000000000" />
<tip>12:</tip>
<name>三制服</name>
<gossip />
</crop>
- <crop id="404" offsetX="0" offsetY="0" seed="Seed" harvestNum="4" isAnimation="0">
<insect position="0,0,0,0,0,0|69,1,80,-28,90,-7|65,-3,87,-47,98,-7|0,0,0,0,0,0" />
<cropGrow value="39600,82800,126000,172800,219600,2000000000" />
<tip>12:</tip>
<name>四制服</name>
<gossip />
循环截取id后面的402 403 404 。截取后显示在List1中。
谢谢了!
以上字符是存在FORM的TEXT1的。
请高手们指点。
加了悬赏了。请高手们帮我一下吧。我要的是代码。代码要注明。

方法1:While循环
Private Sub Command1_Click()
List1.Clear '清空List1
s = "<crop id=""" '设置关键字,根据你提供的文本,应该都是以<crop id="来识别的
l = Len(s) '关键字的长度,下面要用到
p = InStr(Text1, s) '关键字在Text1中出现的位置
Do While p > 0 '开始循环
p1 = InStr(p + l, Text1, """") '查找关键字后的单元格"
If p1 = 0 Then Exit Do '为了冗错,加入该句,意思是如果关键字后没有引号,结束循环,这种可能性是存在的,比如在写网页文件时读取数据库错误,在写了<crop id="后中断了
List1.AddItem Mid(Text1, p + l, p1 - p - l) '截取字符串
p = InStr(p + 1, Text1, s) '重置p值,如果p=0,将结束循环
Loop
End Sub

方法2:For循环
Private Sub Command1_Click()
List1.Clear
arr = Split(Text1, "<crop id=""") '以<crop id="作为分隔符,拆分到数组
For i = 1 To UBound(arr) 'For循环
List1.AddItem Val(arr(i)) '假定ID后为数字,VAL函数可直接截取到非数字的位置,如果后面不是数字,该行修改为:List1.AddItem Left(arr(i), InStr(arr(i), """") - 1)
Next
End Sub

注意以上引号的数量:
a="aaa",a的值为aaa
a="aaa""",a的值为aaa"
a="""",a的值为"
温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-07-11
先用Instr函数搜索【<crop id="】字符串,得到S1值为数值开始偏移量;
然后再用Instr函数搜索刚刚得到搜索位置S1开始搜索【" offsetX=】字符串,得到S2值为数值结束位置;
跟着用Mid函数提取S1+11开始,S2-S1-11为提取长度的字符串,跟着把提取到的字符串用Vol函数转换数字即可。
当然中间要加些防止出错的纠错语句。追问

解决问题的我可以另外加分。

追答

唉,这么简单的问题还要写代码啊?

VERSION 5.00
Begin VB.Form Form1
Caption = "Form1"
ClientHeight = 7770
ClientLeft = 60
ClientTop = 345
ClientWidth = 9375
LinkTopic = "Form1"
ScaleHeight = 7770
ScaleWidth = 9375
StartUpPosition = 3 '窗口缺省
Begin VB.CommandButton Command1
Caption = "Command1"
Height = 375
Left = 4920
TabIndex = 2
Top = 7320
Width = 1335
End
Begin VB.ListBox List1
Height = 7080
Left = 5880
TabIndex = 1
Top = 120
Width = 3255
End
Begin VB.TextBox Text1
Height = 7095
Left = 120
MultiLine = -1 'True
ScrollBars = 3 'Both
TabIndex = 0
Text = "Form1.frx":0000
Top = 120
Width = 5655
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Private Sub Command1_Click()
Dim tempStr As String
Dim S1 As Long, S2 As Long, Len1 As Long
Dim FindStr1 As String, FindStr2 As String, tempFind As String

FindStr1 = "<crop id=" & Chr(34)
FindStr2 = Chr(34) & " offsetX="
tempStr = Text1.Text
Len1 = Len(FindStr1)
Do
S1 = InStr(S1 + 1, tempStr, FindStr1)
If S1 < 1 Then List1.AddItem "查找结束": Exit Do
S2 = InStr(S1 + 1, tempStr, FindStr2)
If S2 < 1 Then List1.AddItem "在文本框里面没找到所需的结束字段": Exit Do
tempFind = Mid(tempStr, S1 + Len1, S2 - S1 - Len1)
List1.AddItem tempFind
Loop

End Sub

把上面的代码保存为文本文件,再把文件名改为form1.frm就可以了。
字数限制注释没戏。

第2个回答  2011-07-11
Option Explicit

Private Function GetStr(ByVal pStr As String) As String
Dim n As Long

n = InStr(pStr, "-")
If n = 0 Then '如果没有-号的情况
GetStr = pStr '我这儿是返回原字符串。你按你的情况自己的定义
Else '如果有-号的情况
n = InStr(n + 1, pStr, "-") '查找第二个-号
If n = 0 Then '没第二个减号,返回原字符串
GetStr = pStr
Else '有第二个减号,返回第二个减号前的字串
GetStr = Mid(pStr, 1, n - 1)
End If
End If

End Function

'测试:
Private Sub Command1_Click()
Dim s As String

s = "0904123-158-1"
Debug.Print GetStr(s)

s = "0904123-158"
Debug.Print GetStr(s)

s = "0904123"
Debug.Print GetStr(s)

End Sub
追问

大哥。对我要截取的字符好像没用。
你只是理论。我要的是代码。

第3个回答  2011-07-11
这个肯定可以,我调试过了:
Private Sub Command1_Click()
Dim a() As String
a = Split(Text1.Text, "- <crop id=")
Dim i As Integer, ss As String
For i = 1 To UBound(a)
ss = Mid(a(i), 2, 3)
list1.AddItem ss
Next
End Sub本回答被提问者采纳
第4个回答  2011-07-11
去看看vb读取xml的相关文章吧!具体就不贴代码了
相似回答