How To: Extract numbers from string

VB.NET

Shared Function ExtractNumbers( ByVal expr As String ) As String
 Return String.Join( Nothing, System.Text.RegularExpressions.Regex.Split( expr, "[^\d]" ) )
End Function

C#

static string ExtractNumbers( string expr )
{
 return string.Join( null,System.Text.RegularExpressions.Regex.Split( expr, "[^\\d]" ) );
}

Call the function as follows

VB.NET

Response.Write ( ExtractNumbers( "12EFR77" ) )

C# 

Response.Write ( ExtractNumbers( "12EFR77" ) );

Published Thursday, August 03, 2006 10:56 PM by SushilaSB
Filed under:

Comments

Monday, September 24, 2007 3:56 PM by Mike Kelly

# re: How To: Extract numbers from string

Thanks, just what I needed!

Thursday, October 11, 2007 6:18 AM by Igmar Iris Francia

# re: How To: Extract numbers from string

Thanks! you saved my neck.

Wednesday, January 09, 2008 4:52 AM by Srinivasan

# re: How To: Extract numbers from string

Can u pls tell me how can we extract decimal number from string.

Thursday, May 15, 2008 4:26 AM by nitin

# re: How To: Extract numbers from string

use this for decimal

string.Join( null,System.Text.RegularExpressions.Regex.Split( expr, "[^.\\d]" ) );

Sunday, August 17, 2008 6:11 PM by Vulfie

# re: How To: Extract numbers from string

This worked perfectly for me and was exactly what I needed to pull an IP Address from a string! Thanks for taking the time to post this method!

Monday, December 08, 2008 12:05 AM by Prabha

# re: How To: Extract numbers from string

How to extract -ve float numbers from a string.

Thursday, June 11, 2009 10:21 AM by viral patel

# re: How To: Extract numbers from string

sabas

its working.

Friday, March 19, 2010 12:31 AM by mash

# re: How To: Extract numbers from string

Its not extracting number from string

Monday, June 14, 2010 1:18 PM by Me

# re: How To: Extract numbers from string

string.Join( null,System.Text.RegularExpressions.Regex.Split( expr, "[^.\\d]" ) );

[^.\\d]

When I type the above into VB.net to get a decimal, it only shows a '.' in the msgbox. Is it the same or is it different for VB.

Tuesday, June 29, 2010 5:54 PM by William Burgos

# re: How To: Extract numbers from string

Decimal in VB.NET:

string.Join( Nothing,System.Text.RegularExpressions.Regex.Split( expr, "[^.\d]" ) )

Thursday, January 27, 2011 12:38 AM by Asif

# re: How To: Extract numbers from string

"Sorry,the rates for the room you selected has been updated.Current Rate is 8977.0"

I need to get 8977 but using the method you have given I will get 89770.

What to do to get 8977.0

Monday, March 14, 2011 4:20 AM by MNassar

# re: How To: Extract numbers from string

this not work with negative Number :(

Thursday, April 14, 2011 4:28 AM by Ana

# re: How To: Extract numbers from string

How I can Extract last 10 Numbers form end of string?

input such as: "d158ghj1234567890sty" or "brf7nb889k1234567890deuh"

output: "1234567890"

Tuesday, June 21, 2011 5:37 AM by Sajid

# re: How To: Extract numbers from string

I want to fetch number from my string

Example:

my string is

"Mr. albert jhon 3423211 bot besan uk"

i want to seprate the number 3423211 ..

Sunday, November 20, 2011 9:34 PM by Ian

# re: How To: Extract numbers from string

Thanks heaps for posting this. Absolute little gem, worked exactly as described!

Wednesday, December 14, 2011 6:26 AM by sajith

# re: How To: Extract numbers from string

we have avoid "" in string value how to do

Thursday, January 05, 2012 3:07 AM by Jumanjee

# re: How To: Extract numbers from string

You just saved my ass!!!Straight on Point..Thanks

Monday, January 09, 2012 11:26 AM by Deepti

# re: How To: Extract numbers from string

Thanks a ton. You're my angel!

Thursday, March 15, 2012 9:11 AM by sushant

# re: How To: Extract numbers from string

thanks for posting. It helped me a lot..

Friday, March 16, 2012 12:53 AM by Silvio

# re: How To: Extract numbers from string

Exactly what i need now, thanks!

Sunday, March 25, 2012 11:56 AM by Bo_newbe

# re: How To: Extract numbers from string

Thank's

Looks promising but maybe I missed something basic?

#include <string.h>

... ?

identifier "Response" is unidentified

identifier "string" is unidentified

identifier "null" is unidentified

identifier "System" is unidentified

I'm using C#

Thanks for any hints!

I hope I'm in the right place with my silly question :-o

Tuesday, April 03, 2012 5:02 PM by Mahesh

# re: How To: Extract numbers from string

Thanks a lot !! this is what I needed !! thanks again :)

Thursday, May 03, 2012 9:45 AM by Ramya

# re: How To: Extract numbers from string

Thanks, the decimal number from string, VB.Net works great. This saved me a lot of time.

Thursday, May 03, 2012 5:13 PM by Kadir

# re: How To: Extract numbers from string

Thanks mate , you saved my day ;)

Friday, May 25, 2012 4:14 AM by leon

# re: How To: Extract numbers from string

I can't get it I am trying to separate

popcorn = RM6.5

from listbox how can I do so?

Thursday, July 05, 2012 3:16 PM by salman_sulaiman_1979

# re: How To: Extract numbers from string

Thanks mate!

really cool! just amazing!

I am falling in love with .NET deeper each day!

thanks again for sharing!

Hugsss

Salman (aka. Sam)

Monday, March 18, 2013 8:57 AM by Mattingly

# re: How To: Extract numbers from string

Hello superb blog! Does running a blog like this require a great deal of

work? I have virtually no expertise in computer programming however I

had been hoping to start my own blog in the near future.

Anyhow, if you have any ideas or techniques for new blog owners please share.

I know this is off topic however I just had to ask.

Thanks!

Sunday, March 24, 2013 8:30 AM by Doe

# re: How To: Extract numbers from string

This page certainly has all the information and facts I wanted about this subject and didn't know who to ask.

Tuesday, April 09, 2013 5:24 AM by Clement

# re: How To: Extract numbers from string

Oahmiue oipuia Natural Erectile Dysfunction Treatment arenoaa icayouqu.