String - ExtractUInt64
A string extension method that extracts the UInt64 from the string.
public static void Main() { string [] input = {"No. 9235125391235", "abc-153846685654"}; foreach(var val in input) { //C# Extension Method: String - ExtractUInt64 var output = val.ExtractUInt64(); Console.WriteLine(output); } }
View Source
using System; using System.Text; public static partial class Extensions { /// <summary> /// A string extension method that extracts the UInt64 from the string. /// </summary> /// <param name="this">The @this to act on.</param> /// <returns>The extracted UInt64.</returns> public static ulong ExtractUInt64(this string @this) { var sb = new StringBuilder(); for (int i = 0; i < @this.Length; i++) { if (Char.IsDigit(@this[i])) { sb.Append(@this[i]); } } return Convert.ToUInt64(sb.ToString()); } }