Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Text;
- using System.Diagnostics;
- namespace ConsoleApplication1
- {
- class Program
- {
- static void Main(string[] args)
- {
- var stopwatch = Stopwatch.StartNew();
- const int times = 100000000;
- for (int i = 0; i < times; i++)
- {
- var output = RemoveNonNumbers1("oa9sd01234567897fa9");
- }
- stopwatch.Stop();
- Console.WriteLine(stopwatch.Elapsed);
- stopwatch.Restart();
- for (int i = 0; i < times; i++)
- {
- var output = RemoveNonNumbers2("oa9sd01234567897fa9");
- }
- stopwatch.Stop();
- Console.WriteLine(stopwatch.Elapsed);
- Console.ReadLine();
- }
- internal static string RemoveNonNumbers1(string p)
- {
- if (string.IsNullOrEmpty(p))
- return p;
- var sb = new StringBuilder();
- foreach (var c in p)
- {
- if (c >= 0x30 && c <= 0x39)
- sb.Append(c);
- }
- return sb.ToString();
- }
- internal static string RemoveNonNumbers2(string p)
- {
- if (string.IsNullOrEmpty(p))
- return p;
- var sb = new StringBuilder();
- foreach (var c in p)
- {
- if (char.IsDigit(c))
- sb.Append(c);
- }
- return sb.ToString();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement