/// <summary> /// Retorna apenas os caracteres alfanuméricos da instância. /// </summary> /// <param name="instance"></param> /// <param name="format"></param> /// <returns></returns> public static string Strip(this string instance, GroupFormat format = GroupFormat.Free) { if (String.IsNullOrEmpty(instance)) { return(String.Empty); } switch (format) { case GroupFormat.Alphabetic: return(new StringBuilder(instance.Length).Append(instance.Where(v => v.IsAlphabetic()).ToArray()).ToString()); case GroupFormat.AlphaNumeric: return(new StringBuilder(instance.Length).Append(instance.Where(v => v.IsAlphaNumeric()).ToArray()).ToString()); case GroupFormat.Numeric: return(new StringBuilder(instance.Length).Append(instance.Where(v => v.IsNumeric()).ToArray()).ToString()); default: return(instance); } }
/// <summary> /// Construtor parametrizado. /// </summary> /// <param name="content"></param> public FormatMask(string content) { _initialValue = content.GetMaskIntialValue(); _content = content.GetMaskContent(); _tokens = _content.GetTokens(); _required = _tokens.Select((v, i) => Tuple.Create(v, i)).Where(t => t.Item1.IsRequired).ToList(); _fillable = _tokens.Select((v, i) => Tuple.Create(v, i)).Where(t => t.Item1.IsFillable).ToList(); _separators = _tokens.Select((v, i) => Tuple.Create(v, i)).Where(t => t.Item1.IsSeparator).ToList(); _formatted = new StringBuilder(_content).Append('|').Append(_initialValue).ToString(); _format = GroupFormat.Empty; foreach (var token in _tokens) { var newFormat = token.Format.GetGroupFormat(); if ((_format == GroupFormat.Empty) || (newFormat == GroupFormat.Free)) { _format = newFormat; } if (_format == GroupFormat.Free) { break; } if (_format == GroupFormat.Numeric) { if ((newFormat == GroupFormat.Alphabetic) || (newFormat == GroupFormat.AlphaNumeric)) { _format = GroupFormat.AlphaNumeric; } } if (_format == GroupFormat.Alphabetic) { if ((newFormat == GroupFormat.Numeric) || (newFormat == GroupFormat.AlphaNumeric)) { _format = GroupFormat.AlphaNumeric; } } } }
/// <summary> /// Indica se o formato de caracteres alfanuméricos. /// </summary> /// <param name="instance"></param> /// <returns></returns> public static bool IsAlphaNumericFormat(this GroupFormat instance) { return(instance == GroupFormat.AlphaNumeric); }
/// <summary> /// Indica se o formato é livre. /// </summary> /// <param name="instance"></param> /// <returns></returns> public static bool IsFreeFormat(this GroupFormat instance) { return(instance == GroupFormat.Free); }