9

Looking for fast/efficient ways to convert an alphanumeric to a number only string

e.g. +123-456/7890 becomes 1234567890 etc.

the existing code is

foreach(char c in str.ToCharArray() )
  if ( char.IsDigit(c) ) stringBuilder.Append(c);

return stringBuilder.ToString();
Saeed Amiri
  • 22,252
  • 5
  • 45
  • 83
Kumar
  • 10,997
  • 13
  • 84
  • 134
  • what are you actually after?... new ideas (eg. use regex), or benchmarks? why are you asking this question - just for info, or is there a real-world scenario? –  Apr 30 '12 at 06:38
  • I don't think your approach is slow.. if measure in speed, should be much faster than regex. The only improvement I can imagine is to replace with Linq, but not in speed, only cleaner code. – xandy Apr 30 '12 at 06:39
  • 1
    Duplicate of http://stackoverflow.com/questions/6361986/how-get-integer-only-and-remove-all-string-in-c-sharp – Imran Rizvi Apr 30 '12 at 06:55
  • @ImranRizvi, this isn't duplicate because he didn't ask for "How to" he asked "Faster than this". – Saeed Amiri Apr 30 '12 at 07:27
  • oh ok, you are right,check I've modified my answer. – Imran Rizvi Apr 30 '12 at 07:48

5 Answers5

5

LINQ Solution:

return new string(str.Where(char.IsDigit).ToArray());

Not sure if it's more efficient; at-least it's not regex!

Andy
  • 1,060
  • 7
  • 13
4
string str="+123-456/7890";
long onlyNumbers= Convert.ToInt64(Regex.Replace(str, @"\D", ""));
Chuck Norris
  • 15,207
  • 15
  • 92
  • 123
  • 1
    @Kumar: And if your number goes beyond `Int32.MaxValue = 2147483647`, use Convert.ToInt64(...); which can give you results till `Int64.MaxValue = 9223372036854775807` – Marshal Apr 30 '12 at 06:43
  • @SaeedAmiri, it's really not as faster, but it's more readable and comfortable to use regexes for me. – Chuck Norris Apr 30 '12 at 06:48
  • @SaeedAmiri: Regex is faster among all the answers given here. Please see my answer for the clarification. – Marshal Apr 30 '12 at 07:17
  • @ChuckNorris: Regex is faster among all the answers given here. Please see my answer for the clarification. – Marshal Apr 30 '12 at 07:18
2

Yes RegEx is faster among others , you can make the comparison even more faster using RegexOptions.Compiled to match the negative/positive cases and reside them apart (if such strings can exists)

e.g.

Regex numberOnlyRegEx = new Regex(@"[^0-9]+", RegexOptions.Compiled);

if (!numberOnlyRegEx.IsMatch(str))
  return 0; //default value;

return Convert.ToInt32(numberOnlyRegEx .Replace(str, "[^0-9]+", ""));
Imran Rizvi
  • 7,331
  • 11
  • 57
  • 101
  • I tested it using Ideone, which gives completely reverse results and now the question would be what to trust VS-IDE or Ideone ? – Marshal Apr 30 '12 at 07:51
  • @Marshal thats strange, I believe more on VS-IDE results as it is on your local machine , but can't say so early. – Imran Rizvi Apr 30 '12 at 07:57
2

Here is an another solution found

string justNumbers = new String(text.Where(Char.IsDigit).ToArray());
int numbers = Convert.ToInt32(justNumbers); 
Marshal
  • 6,551
  • 13
  • 55
  • 91
COLD TOLD
  • 13,513
  • 3
  • 35
  • 52
  • This process is much slower (4 ms) compared to Regex comparisons (1 ms). Although it may not harm too much to the application, (it was just a small 'research') – Marshal Apr 30 '12 at 07:04
1

After seeing many answers which try not to use Regex in this situation, I would like to say actually Regex works much faster in this particular case. I tried calculating time taken for execution using this code snippet

Method suggested by

Edit: These give two completely opposite outputs, i don't know if we should completely trust Ideone than Visual Studio IDE.

Community
  • 1
  • 1
Marshal
  • 6,551
  • 13
  • 55
  • 91
  • Marshal, put your code in [IDEone](http://ideone.com/tH4O4), currently I don't have anything to test, but this place can be acceptable for both of us. – Saeed Amiri Apr 30 '12 at 07:24
  • @SaeedAmiri: Okay I will try to find that too :) – Marshal Apr 30 '12 at 07:29
  • Marshal, just type ideone.com then post your code within it, and left the related link here. – Saeed Amiri Apr 30 '12 at 07:37
  • @Saeed Amiri: I tested it using Ideone, which gives result as you expected, but than the question would be what to trust VS-IDE or Ideone ? – Marshal Apr 30 '12 at 07:50
  • There is not much difference, would you show me your link? I think your test case is not good and we should change it a little. – Saeed Amiri Apr 30 '12 at 07:53
  • @SaeedAmiri Links are posted in my answer. – Marshal Apr 30 '12 at 07:55
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/10691/discussion-between-saeed-amiri-and-marshal) – Saeed Amiri Apr 30 '12 at 07:58
  • Keep in mind, if you are only running the test once (as your snippet implies) you may be seeing the affects of JIT compiler in these times. – Andy May 10 '12 at 00:01