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]" ) );

Leave a Comment

(required) 
(required) 
(optional)
(required)