Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get only numbers from string

Tags:

i want to get only numbers from string.

Lest say that this is my string :

324ghgj123

i want to get:

324123 

what i have tried:

MsgBox(Integer.Parse("324ghgj123")) 
like image 993
Nh123 Avatar asked Nov 13 '12 05:11

Nh123


People also ask

How do you only extract a number from a string in Python?

To find numbers from a given string in Python we can easily apply the isdigit() method. In Python the isdigit() method returns True if all the digit characters contain in the input string and this function extracts the digits from the string.


2 Answers

you can use Regex for this

Imports System.Text.RegularExpressions 

then on some part of your code

Dim x As String = "123a123&*^*&^*&^*&^   a sdsdfsdf" MsgBox(Integer.Parse(Regex.Replace(x, "[^\d]", ""))) 
like image 150
John Woo Avatar answered Sep 30 '22 06:09

John Woo


try this:

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 

or:

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 
like image 33
famf Avatar answered Sep 30 '22 06:09

famf