C# helper to remove all characters that are not digits/numbers 0-9 from the string.
/// <summary>
/// Removes all characters that are not digits 0-9 from the string.
/// </summary>
/// <param name="val">The value to replace.</param>
/// <returns>The <paramref name="val"/> with non-digits removed.</returns>
public static string DigitsOnly(string val)
{
if (string.IsNullOrEmpty(val))
{
return val;
}
var result = new StringBuilder(val.Length);
foreach (var c in val)
{
if (c >= '0' && c <= '9')
{
result.Append(c);
}
}
return result.ToString();
}
//
// SQL CLR....
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
// ReSharper disable CheckNamespace
// ReSharper disable UnusedMember.Global
#pragma warning disable S1118 // Utility classes should not have public constructors
#pragma warning disable S3903 // Types should be defined in named namespaces
public partial class SqlFunctions
{
[SqlFunction(
Name = "DigitsOnly",
DataAccess = DataAccessKind.None,
SystemDataAccess = SystemDataAccessKind.None
)]
public static SqlString DigitsOnly([SqlFacet(MaxSize = 128, IsNullable = true)] SqlString val)
{
return val.IsNull ?
SqlString.Null :
Pods.Common.Utils.StringUtils.DigitsOnly(val.Value);
}
}
#pragma warning restore S1118 // Utility classes should not have public constructors
#pragma warning restore S3903 // Types should be defined in named namespaces