VB中怎么分离字符串中的数字.
在text中输入字符串"e345s454g56s",分离出其中的数字,列在listbox中,怎么写代码?比如输入"ss234t5637u49s34",listbox结果是...
在text中输入字符串"e345s454 g56s",分离出其中的数字,列在listbox中,怎么写代码?
比如输入"ss234t56 37u49 s34",listbox结果是234,56,37,49,34 展开
比如输入"ss234t56 37u49 s34",listbox结果是234,56,37,49,34 展开
3个回答
展开全部
VB可依据ASCII码来判别字符是数字字符的。
ASCII(American Standard Code for Information Interchange,美国标准信息交换代码)是基于拉丁字母的一套电脑编码系统,主要用于显示现代英语和其他西欧语言。它是现今最通用的单字节编码系统,并等同于国际标准ISO/IEC 646。
其中48~57为0到9十个阿拉伯数字。
以下是一段VB查找一个字符串中的数字部分分别添加到ListBox中的代码,忽略了小数点。
Option Explicit
Private Sub Command1_Click()
Dim str As String, tmpChar As String, tmpItem As String
Dim i As Integer, num As Integer
str = "abd134dfq32f3k230"
tmpItem = ""
num = 0
For i = 1 To Len(str) Step 1
tmpChar = Mid(str, i, 1)
If tmpChar >= "0" And tmpChar <= "9" Then
tmpItem = tmpItem + tmpChar
ElseIf tmpItem <> "" Then
AddItemToList (tmpItem)
tmpItem = ""
End If
Next i
AddItemToList (tmpItem)
End Sub
Private Sub AddItemToList(str As String)
If str <> "" Then
List1.AddItem (str)
End If
End Sub
展开全部
Private Sub Command1_Click()
Dim strTest As String
Dim bytArray() As Byte
Dim intcount As Integer
strTest = Text1.Text
bytArray = strTest
For intcount = 0 To UBound(bytArray)
If bytArray(intcount) >= Asc("0") And bytArray(intcount) <= Asc("9") Then
List1.AddItem Chr(bytArray(intcount))
End If
Next
End Sub
================================================
Private Sub Command1_Click()
Dim a() As Byte
Dim s As String
s = ""
a = Text1.Text
For i = 1 To Len(Text1.Text)
a(i) = Asc(Mid(Text1.Text, i, 1))
If a(i) >= Asc("0") And a(i) <= Asc("9") Then
s = s + Chr(a(i))
End If
Next
List1.AddItem s
End Sub
============================
不相隔的数字要分开是吧,底下就可以实现了,用逗号隔开:
Private Sub Command1_Click()
Dim a() As Byte
Dim s As String
s = ""
a = Text1.Text
For i = 1 To Len(Text1.Text)
a(i) = Asc(Mid(Text1.Text, i, 1))
If a(i) >= Asc("0") And a(i) <= Asc("9") Then
If a(i - 1) < Asc("0") Or a(i - 1) > Asc("9") Then
s = s + ","
End If
s = s + Chr(a(i))
End If
Next
If Left(s, 1) = "," Then
s = Right(s, Len(s) - 1)
End If
List1.AddItem s
End Sub
Dim strTest As String
Dim bytArray() As Byte
Dim intcount As Integer
strTest = Text1.Text
bytArray = strTest
For intcount = 0 To UBound(bytArray)
If bytArray(intcount) >= Asc("0") And bytArray(intcount) <= Asc("9") Then
List1.AddItem Chr(bytArray(intcount))
End If
Next
End Sub
================================================
Private Sub Command1_Click()
Dim a() As Byte
Dim s As String
s = ""
a = Text1.Text
For i = 1 To Len(Text1.Text)
a(i) = Asc(Mid(Text1.Text, i, 1))
If a(i) >= Asc("0") And a(i) <= Asc("9") Then
s = s + Chr(a(i))
End If
Next
List1.AddItem s
End Sub
============================
不相隔的数字要分开是吧,底下就可以实现了,用逗号隔开:
Private Sub Command1_Click()
Dim a() As Byte
Dim s As String
s = ""
a = Text1.Text
For i = 1 To Len(Text1.Text)
a(i) = Asc(Mid(Text1.Text, i, 1))
If a(i) >= Asc("0") And a(i) <= Asc("9") Then
If a(i - 1) < Asc("0") Or a(i - 1) > Asc("9") Then
s = s + ","
End If
s = s + Chr(a(i))
End If
Next
If Left(s, 1) = "," Then
s = Right(s, Len(s) - 1)
End If
List1.AddItem s
End Sub
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
dim a() as integer
dim b as string
for i= 0 to 12
a(i)=mid("e345s45 g56s",i,1)
if 9>=a(i)>=0 then
b=a(i)&b
end if
b就是你要的数字
dim b as string
for i= 0 to 12
a(i)=mid("e345s45 g56s",i,1)
if 9>=a(i)>=0 then
b=a(i)&b
end if
b就是你要的数字
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询