PC Review


Reply
Thread Tools Rate Thread

Anyone have a regex expression to create a numeric-only string from a formatted string?

 
 
Ray Stevens
Guest
Posts: n/a
 
      15th Mar 2005
I would like to quickly strip out all non-numeric characters from a string
(including whitespace) into a new string (i.e., remove the edit mask). Would
RegEx be the best way to do this and, if so, what is the syntax?


 
Reply With Quote
 
 
 
 
Nathan Neitzke
Guest
Posts: n/a
 
      16th Mar 2005
Ray,
Regex would probably work, but why not just use a simple loop?

String SomeString = "blah044954blah";
StringBuilder NewString = new StringBuilder();
for (int i = 0;i < SomeString.Length;i++)
{
if (!(((int) SomeString[i]) >= 48 || ((int) SomeString[i]) <= 57)) //
Assuming ASCII of course
{
NewString.Append(SomeString[i]);
}
}
SomeString = NewString.ToString();

This isn't the cleanest code, but it is fast.

Take care.

--
Nathan

"Ray Stevens" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
>I would like to quickly strip out all non-numeric characters from a string
> (including whitespace) into a new string (i.e., remove the edit mask).
> Would
> RegEx be the best way to do this and, if so, what is the syntax?
>
>



 
Reply With Quote
 
Alexander Shirshov
Guest
Posts: n/a
 
      16th Mar 2005
Could be as simple as

Regex.Replace(" 12-34 ", @"\D", String.Empty)

If you don't need decimal or thousand separators, which you most like likely
do need... Then you're on your own :-)

HTH,
Alexander

"Ray Stevens" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
>I would like to quickly strip out all non-numeric characters from a string
> (including whitespace) into a new string (i.e., remove the edit mask).
> Would
> RegEx be the best way to do this and, if so, what is the syntax?
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
convert int to numeric (formatted) string raulavi Microsoft Dot NET 2 11th Mar 2008 02:42 PM
How to evaluate string form of numeric expression in VBA? curiousgeorge408@hotmail.com Microsoft Excel Programming 6 31st Dec 2007 05:23 PM
Giving the String expression for Numeric Values.... Arijit Chatterjee Microsoft Excel Programming 2 19th Nov 2003 10:57 AM
finding all numeric values from a string using Regex Maersa Microsoft Dot NET Framework 1 1st Oct 2003 12:46 PM
Cannot create an object of type 'System.String[]' from its string representation 'String[] Array' for the 'Options' property. Hessam Microsoft C# .NET 0 8th Aug 2003 09:45 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:26 PM.