柚子快報(bào)邀請碼778899分享:替換正則表達(dá)式c#
http://yzkb.51969.com/
1、替換最后一個字符
??? dto.ContactPerson = dto.ContactPerson.ReplaceName("*");
??? /// ??? /// 替換最后一個字符串 ??? /// ??? /// ??? /// ??? /// ??? public static string ReplaceName(this string oldStr, string newStr) ??? { ??????? string pattern = @"(.)$"; ??????? string result = Regex.Replace(oldStr, pattern, newStr);
??????? return result; ??? }
2、替換除第一字符外其他都替換成*
??????? 注意:new string('*', m.Groups[2].Length) 是表示根據(jù)第一個字符后又幾個字符來生成相應(yīng)得*號來替換
?dto.ContactPerson = dto.ContactPerson.ReplaceName();
??? /// ??? /// 替換第一個字符后得其他字符串 ??? /// ??? /// ??? /// ??? /// ??? public static string ReplaceStr(this string oldStr) ??? { ??????? // 正則表達(dá)式:匹配第一個字符之后的所有字符 ??????? string pattern = "(.)(.*)"; ??????? string result = Regex.Replace(oldStr, pattern, m => m.Groups[1].Value + new string('*', m.Groups[2].Length));
??????? return result; ??? }
或則自定義替換成你想要得字符
dto.ContactPerson = dto.ContactPerson.ReplaceStr('-');
??? /// ??? /// 替換第一個字符后得其他字符串 ??? /// ??? /// ??? /// ??? /// ??? public static string ReplaceStr(this string oldStr, char newStr) ??? { ??????? // 正則表達(dá)式:匹配第一個字符之后的所有字符 ??????? string pattern = "(.)(.*)"; ??????? string result = Regex.Replace(oldStr, pattern, m => m.Groups[1].Value + new string(newStr, m.Groups[2].Length));
??????? return result; ??? }
?
3、隱藏手機(jī)號
???????? dto.ContactNumber = PhoneExtensions.GetPhoneHidden(demand.ContactNumber);
???????? /// ??????? /// 獲取隱藏中間四位的手機(jī)號碼 ??????? /// ??????? /// ??????? /// ??????? public static string GetPhoneHidden(this string phone, bool isValidateTelePhone = true) ??????? { ??????????? //如果固化則不隱藏 ??????????? if (isValidateTelePhone && IsTelePhoneNumber(phone)) ??????????? { ??????????????? return phone; ??????????? } ??????????? return Regex.Replace(phone, "(\\d{3})\\d{4}(\\d{4})", "$1****$2"); ??????? }
??????? /// ? ??????? /// 驗(yàn)證固定電話號碼 ? ??????? /// [3位或4位區(qū)號;區(qū)號可以用小括號括起來;區(qū)號可以省略;區(qū)號與本地號間可以用減號或空格隔開;可以有3位數(shù)的分機(jī)號,分機(jī)號前要加減號] ? ??????? /// ? ??????? /// 待驗(yàn)證的字符串 ? ??????? /// 是否匹配 ? ??????? public static bool IsTelePhoneNumber(string input) ??????? { ??????????? string pattern = @"^(((0\d2|0\d{2})[- ]?)?\d{8}|((0\d3|0\d{3})[- ]?)?\d{7})(-\d{3})?$"; ??????????? return IsMatch(input, pattern); ??????? }
柚子快報(bào)邀請碼778899分享:替換正則表達(dá)式c#
http://yzkb.51969.com/
推薦文章