怎样去掉字符串中的字母

za1mb2yc3 2003-04-09 05:45:00
怎样去掉字符串中的字母
...全文
264 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
za1mb2yc3 2003-04-09
  • 打赏
  • 举报
回复
val不可以,只能反回字母左边的数字
za1mb2yc3 2003-04-09
  • 打赏
  • 举报
回复
我用的是
dim i as long
dim str1 as string
str="abcd123"

for i=1 to len(str)
if IsNumeric(Mid(key, i, 1)) then
str1=str1 & mid(str,i,1)
end if
next
msgbox str1
za1mb2yc3 2003-04-09
  • 打赏
  • 举报
回复
谢了,搞定
of123 2003-04-09
  • 打赏
  • 举报
回复
'使用证则表达式的例子:
'引用Microsoft VBScript Regular Expressions library

Dim reg As New RegExp
Dim strTest As String
Dim regPattern As String
Dim Matches As MatchCollection
Dim mtch As Match

'Dim blnFound As Boolean

strTest = "A1234B4567"
regPattern = "\d+$"

With reg
.Pattern = regPattern
If .Test(strTest) Then
Set Matches = .Execute(strTest)

For Each mtch In Matches
strTest = mtch.Value
Next mtch
End If
End With
Set reg = Nothing

顺便说一句,对于字母后面的数字,val()只能返回0.
gang75 2003-04-09
  • 打赏
  • 举报
回复
yes
val()
yo_jo 2003-04-09
  • 打赏
  • 举报
回复
用val()函数
Cooly 2003-04-09
  • 打赏
  • 举报
回复
Str是关键字,不要用关键字做变量,我没有写清楚。不好意思.

Dim i As Long
Dim str1 As String
Strs = "abcd123"

For i = 1 To Len(Strs)
If Asc(Mid(Strs, i, 1)) >= 48 And Asc(Mid(Strs, i, 1)) <= 57 Then
str1 = str1 & Mid(Strs, i, 1)
End If
Next
MsgBox str1
za1mb2yc3 2003-04-09
  • 打赏
  • 举报
回复
不是不能执行,key="a4"
当第二次循时,直接跳过了
of123 2003-04-09
  • 打赏
  • 举报
回复
如果数字一定在右边:
If str Like "*[A-Za-z]*" Then
For i = Len(str) To 1 Step -1
tmp = Mid(str, i)
If Not IsNumeric(tmp) Then Exit For
Next i
str = Mid(tmp, 2)
End If
za1mb2yc3 2003-04-09
  • 打赏
  • 举报
回复
语句有点问题,asc不能正确执行
再看看行吗
Cooly 2003-04-09
  • 打赏
  • 举报
回复
:)
za1mb2yc3 2003-04-09
  • 打赏
  • 举报
回复
太感谢,这么多人,帮我学vb
眼睛都有点湿润了
Cooly 2003-04-09
  • 打赏
  • 举报
回复
也可以用字符串操作

dim i as long
dim str1 as string
str="abcd123"

for i=1 to len(str)
if asc(mid(str,i,1))>=48 and asc(mid(str,i,1))<=57 then
str1=str1 & mid(str,i,1)
end if
next
msgbox str1
Cooly 2003-04-09
  • 打赏
  • 举报
回复
那可以做一个简单的替换啊

for i=65 to 90
strings=replace(strings,chr(i),vbnullstring)
strings=replace(strings,chr(i+32),vbnullstring)
next
za1mb2yc3 2003-04-09
  • 打赏
  • 举报
回复
比如str="abcd123"
我只要123
但数字长度不定长,不能用right()
有时数字是12
za1mb2yc3 2003-04-09
  • 打赏
  • 举报
回复
只剩下数字
Cooly 2003-04-09
  • 打赏
  • 举报
回复
去掉字符串中的字母,你要留下什么?

7,759

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧