只从字符串中获取数字 [英] Get only numbers from string

查看:31
本文介绍了只从字符串中获取数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想从字符串中获取数字.

i want to get only numbers from string.

不要说这是我的字符串:

Lest say that this is my string :

324ghgj123

我想得到:

324123

我尝试过的:

MsgBox(Integer.Parse("324ghgj123"))

推荐答案

试试这个:

Dim mytext As String = "123a123"
Dim myChars() As Char = mytext.ToCharArray()
For Each ch As Char In myChars
     If Char.IsDigit(ch) Then
          MessageBox.Show(ch)
     End If
Next

或:

Private Shared Function Num(ByVal value As String) As Integer
    Dim returnVal As String = String.Empty
    Dim collection As MatchCollection = Regex.Matches(value, "\d+")
    For Each m As Match In collection
        returnVal += m.ToString()
    Next
    Return Convert.ToInt32(returnVal)
End Function

这篇关于只从字符串中获取数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆