public string ApplyMask(string text, MaskRules rule) { string temp = ""; if (rule.Mask != "") { // adjust length of greater than last mask entry if (text.Length > this.MaxLengthFromMask) { text = text.Substring(0, this.MaxLengthFromMask); } var result = System.Text.RegularExpressions.Regex.Match(rule.Mask, CV_defaultMask); temp = rule.Mask; do { if (System.Text.RegularExpressions.Regex.Match(result.Value, CV_defaultOneMask).Success || String.IsNullOrEmpty(result.Value)) { // result is an empty string. return. done parsing if (String.IsNullOrEmpty(result.Value)) { break; } // end match var obj = ConvertMatch(result.Value); temp = temp.Replace(result.Value, text.Substring(obj[0])); break; } else { var obj = ConvertMatch(result.Value); if (obj[0] + obj[1] >= text.Length) { // keeping format character at end?? var t = result.Value[result.Value.Length - 1]; temp = temp.Replace(result.Value, text.Substring(obj[0])); } else { temp = temp.Replace(result.Value, text.Substring(obj[0], obj[1])); } result = result.NextMatch(); } } while (true); return(temp); } return(text); }
public string ApplyMask(string text, MaskRules rule) { string temp = ""; if (rule.Mask != "") { // adjust length of greater than last mask entry if (text.Length > this.MaxLengthFromMask) { text = text.Substring (0, this.MaxLengthFromMask); } var result = System.Text.RegularExpressions.Regex.Match(rule.Mask, CV_defaultMask); temp = rule.Mask; do { if (System.Text.RegularExpressions.Regex.Match(result.Value, CV_defaultOneMask).Success || String.IsNullOrEmpty(result.Value)) { // result is an empty string. return. done parsing if (String.IsNullOrEmpty(result.Value)) break; // end match var obj = ConvertMatch(result.Value); temp = temp.Replace(result.Value, text.Substring(obj[0])); break; } else { var obj = ConvertMatch(result.Value); if (obj[0] + obj[1] >= text.Length) { // keeping format character at end?? var t = result.Value[result.Value.Length-1]; temp = temp.Replace(result.Value, text.Substring(obj[0])); } else { temp = temp.Replace(result.Value, text.Substring(obj[0], obj[1])); } result = result.NextMatch(); } } while (true); return temp; } return text; }