ToNumericString

using System; using System.Text; public static class MyExtensionMethods { public static string ToNumericString(this string str) { if (str == null) { // If source is null, returns null. return null; } if (string.IsNullOrEmpty(str)) { // If source is empty, returns the source string. return str; } // StringBuilder that joins the numeric chars found in the string. StringBuilder sbOnlyNumbers = new StringBuilder(); foreach (char c in str) { // Verifies every char. if (char.IsNumber(c)) { // Only add the char to StringBuilder if the char is numeric. sbOnlyNumbers.Append(c); } } // Returns only the numeric chars found in the source string. return sbOnlyNumbers.ToString(); } }
Select only the numeric chars from some string.

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.